listing directory or folder contents

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
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

listing directory or folder contents

Post by ryanfern86goa »

hi there i am trying to list directory or a folder...i get the list with this code below but when i put it in a function it doesnt.. any ideas whats going wrong?...


$dir="images";
$dirs = scandir($dir);
foreach ($dirs as $file)
{
echo$file;

}
this above code works but the below doesnt
list("images");
function list($dir){
$dirs = scandir($dir);
foreach ($dirs as $file)
{
echo$file;

}
}

any help would be appreciated

thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: listing directory or folder contents

Post by requinix »

What's wrong is that you didn't explain what "doesn't work" means.

And you used the list() construct as the name of a function.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: listing directory or folder contents

Post by SimpleManWeb »

Well, the first thing I would ask is where is your function being run from? Is it in the same directory as the "images" folder? If not, then you would need to use "../" appropriately to navigate to the correct folder. Other than that, I doubt it has anything to do with folder permissions, but you should check that too.

Hope this helps.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: listing directory or folder contents

Post by manohoo »

list is a language construct in PHP, so you can't use it as a function name.

http://php.net/manual/en/function.list.php
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: listing directory or folder contents

Post by ryanfern86goa »

hi tasairis and manohoo

thanks for pointing that out. and really sory for not making it clear.

i got it working it prints all the files now,
but now i came across another problem. after i run this script it only says ". "and" .." are directories and cosiders or prints all folders in images as "file is not a directory"

the folder i want to scan is "images" and it contains pics and subfolders.php scan script is outside "images" folder


filesdisplay("images/");
function filedislay($dir){
$dirs = scandir($dir);
foreach ($dirs as $file)
if (is_dir($file))
{
echo"file is a directory ".$file;
}
else{
echo"file is not a directory ".$file;
}
}
thanks
Post Reply