There are many ways to retrieve the contents of a folder, for example glob , scandir , DirectoryIterator and RecursiveDirectoryIterator , and I recommend that you check DirectoryIterator as it has great potential.
Example using the scandir method
$dirname = getcwd(); $dir = scandir($dirname); foreach($dir as $i => $filename) { if($filename == '.' || $filename == '..') continue; var_dump($filename); }
DirectoryIterator class example
$dirname = getcwd(); $dir = new DirectoryIterator($dirname); foreach ($dir as $path => $splFileInfo) { if ($splFileInfo->isDir()) continue;
The following is a less common example using the RecursiveDirectoryIterator class:
//use current working directory, can be changed to directory of your choice $dirname = getcwd(); $splDirectoryIterator = new RecursiveDirectoryIterator($dirname); $splIterator = new RecursiveIteratorIterator( $splDirectoryIterator, RecursiveIteratorIterator::SELF_FIRST ); foreach ($splIterator as $path => $splFileInfo) { if ($splFileInfo->isDir()) continue; // do what you have to do with your files //example: get filename var_dump($splFileInfo->getFilename()); }
Nazariy
source share