PHP

List all files in a directory and return an array.



List all files in a directory and return an array.

function dirList ($directory) {
// create an array to hold directory list
$results = array();

// create a handler for the directory
$handler = opendir($directory);

// keep going until all files in directory have been read
while ($file = readdir($handler)) {

// if $file isn’t this directory or its parent,
// add it to the results array
if ($file != ‘.’ && $file != ‘..’)
$results[] = $file;
}

// tidy up: close the handler
closedir($handler);

// done!
return $results;
}

Share

Thanks for reading my blog. If you like what I write, why not subscribe to my feed?

If you are busy, I can send the latest post to your email. Just subscribe to my email updates.

 

Enter your email address:

Delivered by FeedBurner

Discussion

No comments for “List all files in a directory and return an array.”

Post a comment