Reading through a directory with PHP
Posted June 18th, 2009 in PHP
PHP's opendir() readdir() and closedir() are used for reading the contents of a directory (there's also glob() and scandir() but I'll cover those in later posts). Combined with functions like is_file() is_dir() and is_link() and you can easily build up a directory tree or process files etc.
Example directory
The examples below look at a directory with the following:
bar.txt A regular file baz A directory foo.txt A regular file link2foo.txt A symbolic link to foo.txt
Simple example
A simple example to loop through the directory's contents and display the names of the files, directories etc within it is as follows:
$handle = opendir("/path/to/directory");
while($name = readdir($handle)) {
echo "$name\n";
}
closedir($handle);
The output for the example directory is:
foo.txt .. bar.txt . baz link2foo.txt
Note that it's not sorted in any particular way and includes . and .. which are indicators for the current directory and parent directory.
Sorting the data
Instead of echoing out the filenames they could instead be stored in an array, sorted and then some other processing done on them. This will ensure they are in alphabetical order.
$handle = opendir("/path/to/directory");
$names = array();
while($name = readdir($handle)) {
$names[] = $name;
}
closedir($handle);
sort($names);
Doing print_r($names) will show this:
Array
(
[0] => .
[1] => ..
[2] => bar.txt
[3] => baz
[4] => foo.txt
[5] => link2foo.txt
)
Testing what type of file it is
The final example loops through the directory and checks whether it's a file, directory or symbolic link and does some processing depending on which type it is (in this example it's simply echoing out what type it is and the name). The additional if($name != '.' && $name != '..') text excludes the special . and .. directory names.
$dir = "/path/to/dir";
$handle = opendir($dir);
while($name = readdir($handle)) {
if(is_dir("$dir/$name")) {
if($name != '.' && $name != '..') {
echo "directory: $name\n";
}
}
elseif(is_link("$dir/$name")) {
echo "link: $name\n";
}
else {
echo "file: $name\n";
}
}
closedir($handle);
And the output:
file: foo.txt file: bar.txt directory: baz link: link2foo.txt
Next posts in this series
To follow up this post I'll look at using scandir() and glob().
Related posts:
- Get the directory name and filename from a full path name with PHP (Thursday, December 10th 2009)
- Using PHP's scandir() function to find files in a directory (Friday, July 17th 2009)
- Using PHP's glob() function to find files in a directory (Thursday, July 2nd 2009)
- Get the included files with PHP (Thursday, June 4th 2009)
- Create a file with a unique name with PHP (Thursday, February 28th 2008)
Subscribe / Follow / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email to have my posts sent in a daily email, follow me on Twitter or follow me on Facebook.
At least one new post is usually made every day. See my posting schedule for more details.
