Tommy D’s Sexy Blog

TagLine Here

PHP Tip using opendir() and readdir()

Filed under: PHP Tips — Tommy D. at 12:06 pm on Saturday, June 25, 2005

Sometimes you may require a list of all of the files in a directory. Such a list can be built using the opendir() and readdir() functions:

<?php
$targetdir = “/var/tmp”;
$files = array();
$directory = opendir($targetdir);
while($filename = readdir($directory)){
#ignore . and ..
if(strlen($filename) > 2){
array_push($files, $filename);
}
}
?>

Upon execution, $files will be an array of filenames (basename only, with no path). If your directory contains files whose names are one or two characters long, you’ll need to replace the strlen() test with a comparison against the literals ‘.’ and ‘..’ in order to avoid adding the current and parent directory names to the array.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

 

Videos, Slideshows and Podcasts by Cincopa Wordpress Plugin