Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created February 20, 2011 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/835648 to your computer and use it in GitHub Desktop.
Save joshhartman/835648 to your computer and use it in GitHub Desktop.
List Certain Types of Files in a Directory
<?php
$folder = opendir('.'); // Use '.' only if the PHP file is in the same folder as your files, otherwise use a relative or absolute path.
$file_types = array('jpg', 'jpeg', 'gif', 'png');
$file_list = array();
while ($file = readdir ($folder)) {
$parts = explode('.', $file);
$ext = array_pop($parts);
if(in_array($ext, $file_types)) {
array_push($file_list, $file);
}
}
closedir($folder);
print_r($file_list);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment