Loop code for each file in a directory

189,004

Solution 1

scandir:

$files = scandir('folder/');
foreach($files as $file) {
  //do your work here
}

or glob may be even better for your needs:

$files = glob('folder/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
  //do your work here
}

Solution 2

Check out the DirectoryIterator class.

From one of the comments on that page:

// output all files and directories except for '.' and '..'
foreach (new DirectoryIterator('../moodle') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}

The recursive version is RecursiveDirectoryIterator.

Solution 3

Looks for the function glob():

<?php
$files = glob("dir/*.jpg");
foreach($files as $jpg){
    echo $jpg, "\n";
}
?>

Solution 4

Try GLOB()

$dir = "/etc/php5/*";  

// Open a known directory, and proceed to read its contents  
foreach(glob($dir) as $file)  
{  
    echo "filename: $file : filetype: " . filetype($file) . "<br />";  
}  

Solution 5

Use the glob function in a foreach loop to do whatever is an option. I also used the file_exists function in the example below to check if the directory exists before going any further.

$directory = 'my_directory/';
$extension = '.txt';

if ( file_exists($directory) ) {
   foreach ( glob($directory . '*' . $extension) as $file ) {
      echo $file;
   }
}
else {
   echo 'directory ' . $directory . ' doesn\'t exist!';
}
Share:
189,004
Chiggins
Author by

Chiggins

PHP, C#, Android, Java, C++, Qt, HTML, CSS, Javascript, SQL, Linux, IIS, Apache

Updated on June 22, 2020

Comments

  • Chiggins
    Chiggins about 4 years

    I have a directory of pictures that I want to loop through and do some file calculations on. It might just be lack of sleep, but how would I use PHP to look in a given directory, and loop through each file using some sort of for loop?

    Thanks!

  • Ajibola
    Ajibola over 11 years
    the filetype doesn't work, it returns 'file'.
  • Zero3
    Zero3 about 9 years
    Is the $files variable used to avoid calling scandir() more than once in the foreach loop? Or can I embed it directly without any side effects?
  • Emil Vikström
    Emil Vikström about 9 years
    @Zero3, the variable is used for readability only. PHP will always work with a copy of the array in any foreach loop, which means that scandir() will be called only once. (And since PHP uses copy-on-write you won't get any performance problems with this "copying" either). Yes, you can embed the scandir() call without any performance drawbacks.
  • mn.
    mn. about 9 years
    N.B.: scandir() includes '.' & the '..' nodes. If you don't want to go through them, add a check/delete them from the scandir() result
  • vallentin
    vallentin almost 9 years
    @Ajibola how about we check the documentation before saying something doesn't work, huh? filetype() returns the type of the file. Thereby possible results would be file, dir, char, block, .... You can use something like mime_content_type() if you want to know the content type of the file.
  • Daniel Waters
    Daniel Waters about 7 years
    This should be the top answer, far more useful and a more modern approach than scandir or glob