Archived

This topic is now archived and is closed to further replies.

Promit

(PHP) How to list everything in a directory?

Recommended Posts

Promit    13246
I want to write a PHP script that''ll list all of the files, directories, etc. in the directory of the PHP script. I don''t really want an entire script for copy pasting, just tell which functions to use in order to get a list of files in a directory. Oh, and enabling Apache directory contents listing is not an option.

Share this post


Link to post
Share on other sites
Extrarius    1412
Be carefull when you do that - you probably want to filter out all executable files(meaning files with +x if you''re using linux), plus scripts, hidden files (that start with a . if in linux), etc. Otherwise such a script could be a security risk.

If you let the user select a directory (either in a form or on the URL line) you probably want to make sure it is something at the same depth as the script or below so they don''t go browsing places you don''t want them to.

Share this post


Link to post
Share on other sites
TheBaker    122

if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo $file;
}
}
closedir($handle);
}
?>


That code will list the contents of what ever directory the file is in. To change the directory, simply change the opendir('.') to opendir('directory/name/').

[edited by - TheBaker on March 27, 2004 2:08:53 PM]

Share this post


Link to post
Share on other sites
Promit    13246
TheBaker: I told you I didn''t want an entire script.

In any case, I''m using scandir instead of opendir, but thanks for pointing me in the right direction.

Share this post


Link to post
Share on other sites
Colin Jeanne    1114
quote:
Original post by Promit
TheBaker: I told you I didn't want an entire script.

In any case, I'm using scandir instead of opendir, but thanks for pointing me in the right direction.

scandir() only exists in PHP 5. If you want your script to run on current systems (that are not yours) this function is not an option. If you're ok with that or knew that already then nevermind!


Thanks Salsa!Colin Jeanne | Invader's Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

[edited by - Invader X on March 27, 2004 5:40:08 PM]

Share this post


Link to post
Share on other sites