function to grab file name from a folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BlueFlame
Forum Commoner
Posts: 33
Joined: Wed Oct 12, 2005 5:21 am

function to grab file name from a folder

Post 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.
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Check out the glob function also

http://us3.php.net/function.glob
BlueFlame
Forum Commoner
Posts: 33
Joined: Wed Oct 12, 2005 5:21 am

Post by BlueFlame »

Thanks. That glob function looks really handy compaired to the opendir() but i shell stick with SKDevelopment's code.
Post Reply