Need help with directory listings and dirs

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
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

Need help with directory listings and dirs

Post by pHaZed »

Hey,
I have written a Directory listing script, Which is in a function with the 3rd argument of the function being $showdir
eg: function displayFiles ($path, $extension, $showdir)
then its called like:
displayFiles ("/home/wwwroot/curves/wallpapers", "jpg", "0");

I wish to be able to choose whether or not to display dirs, Thats wat the 3rd argument is for($showdirs)...
If anyone can tell me how with an if/else condition i can do
if($showdir == 1)
Any ideas on the code to display or not to display dirs?
User avatar
cwcollins
Forum Commoner
Posts: 79
Joined: Thu May 16, 2002 3:51 pm
Location: Milwaukee, WI, USA

Directory listing script

Post by cwcollins »

i would loop through the listing and do a

Code: Select all

if(!is_dir(їfile_or_directory_name])){echo їfile_or_directory_name];}
the is_dir() function is doc'd at :

http://www.php.net/manual/en/function.is-dir.php
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Code: Select all

if(!is_dir(їfile_or_directory_name]) || $isdir == 1){
echo їfile_or_directory_name];
}
And improvement of the above code.

Cheers Sam
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

i think he wants to be able to turn it off and on

which would be like

if($show_dir == "1")){

statements to show the directory

} else {

statements to execute if the directory is not going to be shown

}
User avatar
cwcollins
Forum Commoner
Posts: 79
Joined: Thu May 16, 2002 3:51 pm
Location: Milwaukee, WI, USA

Post by cwcollins »

ok, the final answer:

function start :::

Code: Select all

function whatever($directory,$extention,$showdir){
::: loop through directoy listing, and use this to check

Code: Select all

if(!is_dir(їfile_or_directory_name]) || $showdir == 1){ 
echo їfile_or_directory_name]; 
}
::: end loop

Code: Select all

}
end function :::

if $showdir is set to 1, it will display all entries, including directories, if not, it will ignore directories (i.e. entries that return false from the is_dir()).

the way that hob_goblin indicated will work, but why have an extra if...else? If hob_goblin's way is easier for you, use the code block that i put in the first response.

that either helped, or confused the hell out of you. good luck.

-c.w.collins
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

Post by pHaZed »

Thanks everyone,
That all really helped. I know have quite an impressive archive script for my site :D
Post Reply