Page 1 of 1

function to grab file name from a folder

Posted: Sun Feb 12, 2006 5:23 am
by BlueFlame
Im looking for a function or a way to grab all the file names in a certain folder and chuck them into an array, does any one know how to do this.

Posted: Sun Feb 12, 2006 8:00 am
by SKDevelopment
It would be something like this.

Code: Select all

$directory = "directory1"; // directory name
$dir = opendir($directory);
while(($file = readdir($dir)))
{
  if(is_file($directory.'/'.$file))
  {
   $a[] = $file;
  }
} // end while
closedir($dir); 
// $a containes all the file names in the direstory
echo "<pre>"; print_r($a); echo "</pre>";
The array $a will contain all the file names in the folder $directory.

--
Best Regards,
Sergey Korolev
www.SKDevelopment.com

Posted: Sun Feb 12, 2006 9:10 am
by hawleyjr
Check out the glob function also

http://us3.php.net/function.glob

Posted: Sun Feb 12, 2006 3:06 pm
by BlueFlame
Thanks. That glob function looks really handy compaired to the opendir() but i shell stick with SKDevelopment's code.