php glob() not finding any files - php

$directory = $_SESSION['base_url'] . "assets/images/*.jpg";
$images = glob($directory);
This code is not working. If I print out $directory, it matches the directory where I have put some .jpg files. $images just remains empty...
As bsdnoobz answered, this was my solution:
$directory = dirname(dirname(dirname(__FILE__))) . "\assets\images\\"; //escape
$images = glob($directory . "*.jpg");

It looks like $_SESSION['base_url'] points to your app's URL (like http://example.com). You should use filesystem path instead of URL. Try something like this:
$directory = dirname(__FILE__) . '/assets/images/*.jpg';

$directory = $_SESSION['base_url'] . "assets/images/*.jpg";
$images = glob($directory);
There was an additional '.

You have an extra ' between the / and the .jpg

Related

How to get filename from image pulled in through glob directory [duplicate]

How can I just return the file name. $image is printing absolute path name?
<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
$images = glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image)
echo $image
?>
All I want is the file name in the specific directory not the absolute name.
Use php's basename
Returns trailing name component of path
<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
$images = glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image)
echo basename($image);
?>
Instead of basename, you could chdir before you glob, so the results do not contain the path, e.g.:
<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
chdir($directory); // probably add some error handling around this
$images = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image)
echo $image;
?>
This is probably a little faster, but won't make any significant difference unless you have tons of files
One-liner:
$images = array_map('basename', glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE));
Use basename()
echo basename($image);
You can also remove the extension like this:
echo basename($image, '.php');
Take a look at pathinfo
http://php.net/manual/en/function.pathinfo.php
Pretty helpful function
Example extracting only file names and converting in new array of filenames width extension.
$dir = get_stylesheet_directory();//some dir - example of getting full path dir in wordpress
$filesPath = array_filter(glob($dir . '/images/*.*'), 'is_file');
$files = array();
foreach ($filesPath as $file)
{
array_push($files, basename($file));
}
If you're nervous about the unintended consequences of changing the working directory, you can store the current dir with getcwd() before changing, then simply use that value to switch back after you've done everything you need to do in that dir.
<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
$working_dir = getcwd();
chdir($directory);
$files = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
chdir($working_dir);
?>

renaming images in a folder - images deleted

I need to rename images in a folder i.e. give them unique names. Using this code images are not renamed but deleted !
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach($items as $img) {
$uniq = uniqid() . '.jpg';
rename("$img", "$uniq");
}
Your code is moving your images to a different directory.
Add the path to your unique name.
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach($items as $img) {
$uniq = $path . uniqid() . '.jpg';
rename("$img", "$uniq");
}
Here you must provide complete path or a valid path. In your code you are trying to rename files in current working directory but actually you have to work on ../b-300x250/ directory, So you should append this to make your code to rename a file correctly.
Change this:
rename("$img", "$uniq");
This:
rename("$img", $path.$uniq);
PHP code:
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach ($items as $img)
{
$uniq = uniqid() . '.jpg';
rename("$img", $path.$uniq);
}

using 'glob' to get all images from a directory

I'm trying to get all images from a directory and print them. I know my URL is correct because I am echoing the the $dirname along with a .jpg image I have saved in the directory. What am I doing wrong?
<?php
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'"/>';
}
//check to see if $dirname is correct
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
?>
//edit
I have solved my problem, by removing the jpg extention and just pulling "*" for everything in the folder. Although it works for me now, its not best practice. It must be something to do with different .jpg file formats?
You used lowercase file extension here.
$images = glob($dirname . "*.jpg");
And uppercase JPG in your test output.
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
If you're using a linux system then file extensions are case sensitive.
You need to prepend the dirname to glob's result to get the full paths.
So something like:
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$dirname.$image.'"/>';
}

Symfony access folder image in web directory

I am using Symfony to build a site, and I have a little problem. My images are stored in /web/images/ and I am wondering how I can use this php function glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); to get the list of the files. Actually, it returns an empty array.
Thanks in advance. Here is the code I am using directly from the controller :
$imagesDir = $this->get('kernel')->getRootDir() . '/../web/images';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$json=json_encode($images);
Your $imagesDir does not have a trailing slash, glob() doesn't add it it just searches for files with name images*.{jpg,jpeg,png,gif}', change to:
$imagesDir = $this->get('kernel')->getRootDir() . '/../web/images/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$json=json_encode($images);
and it should work.

List images(01.png) and descriptions(01.txt) from directory

How can I display images from directory and get a corresponding description with each image, give the description exists.
in Directory //
01.png
01.txt
02.png
03.png
03.txt
etc.
to display as //
<img src="01.png"><br>This is the description from the text file named 01.txt
<img src="02.png"><br>
<img src="03.png"><br>This is the description from the text file named 03.txt
I've been searching and searching, but can't find anything, so if someone could point me in the right direction it would be greatly appreciated. Also this is a very useful thing to be able to do for people wanting to create very simple galleries or lists of images and names.
Thanks in advance!
This is what you're looking for, as the description must be dynamically captured from a corresponding .txt file:
$dir = './';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
$filename = pathinfo( $file, PATHINFO_FILENAME) . '.txt';
$description = file_exists( $filename) ? file_get_contents( $filename) : '';
echo '<img src="' . $file . '"><br>' . $description;
}
What it does is grabs an array of *.png files using glob() from a given directory ($dir). Then, for each image, it gets the filename of the image (so 01.png would be 01), and appends .txt to get the name of the description file. Then, it loads the description file into the $description variable using file_get_contents() if the description file exists. It then outputs the desired HTML.
I assume you have the .php file in the same directory as the pictures and text files are.
You could use function glob() to read in all image-files as array from the directory, cut off the file extension (so '01.png' becomes '01') and append the file extension with string concatentation.
A working code example may look like this:
<?php
$path_to_directory = './';
$pics = glob($path_to_directory . '*.png');
foreach($pics as $pic)
{
$pic = basename($pic, '.png'); // remove file extension
echo '<img src=\"{$pic}.png\"><br>';
if(file_exists($pic . '.txt'))
{
echo file_get_contents("{$pic}.txt");
}
}
?>
So definitely have a look on these functions:
http://www.php.net/glob
http://www.php.net/basename
http://www.php.net/file_get_contents
Happy coding.
Your question is a bit confusing.
make an array with all information.
$pics = array('img' => '01.png', 'text' => 'This is the description');
foreach($pics as $pic) {
echo '<img src="'.$pic['name'].'" alt="">' . $pic['text'];
}
So you have to put your information in an array or a database otherwise you cannot map the desciption to your image.
When you want to read dynamicly the folder its a bit difficult.
You can look at readdir or glob then you can read all images get the name and load the textfile with file_get_contents but i think its not a really performant way.
Code Modified from here : http://php.net/manual/en/function.readdir.php
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
//print each file name
foreach($images as $image)
{
print "<img src=\"$image\"><br>This is the description from the text file named $image";
}
ok, so this won't print the contents of the text file, but I'm sure you can further modify the code above to figure that out
YEP
Updated version of ZnArKs code, as he missed that you wanted the content of the files
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.png");
//print each file name
foreach($images as $image)
{
$textfile = substr($image, 0, -3) . "txt";
echo "<img src='{$image}'><br/>";
if(file_exists($textfile))
{
echo file_get_contents($textfile) . "<br/>";
}
}

Categories