How to Work With Directories in PHP
PHP offers a number of different functions to work with the local directory structure. There are at least three fairly simple ways to create and output a list of files in a directory.
Like the file functions, each method is slightly different. Knowing how each operates will help you choose which function to use when you want to work with directories in your PHP script.
PHP 4 - opendir, readdir, closedir
The traditional method of working with directories is very similar to the traditional method of working with files. You open a directory and save it as a resource in a variable. Then, you read through it until the end. Finally, you close it to tidy things up.
$dirName = 'images'; $dir = opendir($dirName); while ($filename = readdir($dir)) { echo $filename . '<br />'; } closedir($dir);
That simple script will open a directory (’images’), read every filename in the directory, and then clean up the directory resource.
The readdir() function goes through the files as they are stored in the filesystem. There’s no real logic to their order - they just come the way they are. Each time you use readdir(), the pointer in the $dir resource is moved to the next file.
PHP 5 - A Convenience Function, scandir
In PHP 5, a new convenience function was introduced to handle all of the major directory functions with one call. Similar to the way file_get_contents handles all of the file writing, scandir handles all of the directory listing.
With a single function, scandir opens a given directory, reads all of the files into an array, and closes the directory.
$dirname = 'images'; $filenames = scandir($dirname); foreach ($filenames as $file) { echo $file . '<br />'; }
This slightly shorter snippet of code accomplishes the same thing as the original example. As an added bonus, the filenames are already stored in an array ($filenames). The filenames are also sorted alphabetically.
You could of course achieve the same end with the original directory functions - but you’d have to add more lines of code to store the data in an array and then sort it. One of the biggest reasons to use scandir() is that it’s a convenience.
There’s also one functional difference between the two. scandir() never actually accesses a file - so it’s “Last Accessed Time” will remain the same. When you use readdir() to cycle through a directory, each file is accessed - resetting the “Last Accessed Time.”
This often won’t matter, but it could play an important role in a script that works directly with a file’s “Last Accessed Time.”
The Versatile Choice - glob
The most versatile choice is glob(). Like scandir, it returns an array of filenames. However, glob allows you to specify a pattern to match - so only the matching filenames are returned.
The pattern follows all of the rules for wildcards when using a command like dir. For example, this will get all of the files in the ‘images’ directory.
$dirName = 'images'; $filenames = glob($dirName . '/*.*'); foreach ($filenames as $file) { echo $file . '<br />'; }
There is a very important difference between this and the other functions, though.
glob() returns paths, not filenames. In this case, the output will be something like ‘images/image.jpg,’ not just ‘image.jpg.’ The directory listing is based on the current working directory - so specifying a path (like we did) will cause that directory to be included in the returned path.
If you don’t want the full path shown, you can use some other functions to change the working directory - like using ‘cd’ in the command line. By default, the working directory is where the script is executing. In this case, we could change the working directory to ‘images’ to get filenames without the directory as part of the path.
chdir('images');
You can also use the GLOB_BRACE flag to pass multiple file extensions. For example, this would allow you to find all images with extension .jpeg, .png, or .gif.
chdir ('images'); $filenames = glob('*.{jpeg,png,gif}', GLOB_BRACE); // Notice there are NO spaces after the commas
glob() can be quite a powerful tool, indeed. If all you want is a list of files in a directory, you’ll probably want to stick to scandir() - so you don’t have to go messing with the working directory.
For anything else - like a selective list of files - you should make use of glob’s matching functionality rather than sorting through the filenames yourself.
Tags: coding, directory, file system, php, snippet







Leave a Reply