124

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!

Chiggins
  • 8,197
  • 22
  • 56
  • 81

5 Answers5

297

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
}
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
  • 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? – Zero3 May 10 '15 at 15:57
  • 2
    @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. – Emil Vikström May 11 '15 at 12:07
  • 2
    N.B.: scandir() includes '.' & the '..' nodes. If you don't want to go through them, add a check/delete them from the scandir() result – mn. Jun 25 '15 at 14:16
  • 1
    Warning: `GLOB_BRACE` is not available on musl libc systems such as Alpine Linux. – Martin Braun May 12 '23 at 20:28
77

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.

squirrel
  • 2,010
  • 14
  • 10
12

Looks for the function glob():

<?php
$files = glob("dir/*.jpg");
foreach($files as $jpg){
    echo $jpg, "\n";
}
?>
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
fvox
  • 1,077
  • 6
  • 8
3

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!';
}
TURTLE
  • 3,728
  • 4
  • 49
  • 50
3

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 />";  
}  
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • the filetype doesn't work, it returns 'file'. – Ajibola Mar 22 '13 at 10:22
  • 1
    @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. – vallentin Jul 24 '15 at 09:08