PHP Listing files in a directory
There are numerous variations to loop through a list of files in a directory in PHP. Here are a couple of simple ways:
Variation 1:
$folder = dir("/path/to/folder");
while($folderEntry = $folder->read()) {
// $folderEntry is the resource. You can now check against it.
}
Variation 2:
if ($handle = opendir("/path/to/folder")) {
while (false !== ($file = readdir($handle))) {
// $file is the resource
}
}
Both use assignment in a while loop to not only get the resource but terminate looping through your results. Variation 2 includes a condition to see if there were issues while openning the directory. You should consider how you will deal with these in your code.





