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
listing directory or folder contents
Moderator: General Moderators
-
ryanfern86goa
- Forum Newbie
- Posts: 16
- Joined: Tue Sep 22, 2009 5:26 pm
Re: listing directory or folder contents
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.
And you used the list() construct as the name of a function.
- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: listing directory or folder contents
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.
Hope this helps.
Re: listing directory or folder contents
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
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
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
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