Page 1 of 1

listing directory or folder contents

Posted: Mon Jan 11, 2010 4:43 pm
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

Re: listing directory or folder contents

Posted: Mon Jan 11, 2010 5:48 pm
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.

Re: listing directory or folder contents

Posted: Mon Jan 11, 2010 6:24 pm
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.

Re: listing directory or folder contents

Posted: Mon Jan 11, 2010 6:42 pm
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

Re: listing directory or folder contents

Posted: Tue Jan 12, 2010 4:50 am
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