Click to See Complete Forum and Search --> : Display files in Directory
comptech520
07-07-2006, 02:53 AM
hello
I have this code to display all of the files in a directory.
But if there is a folder in the directory it will not display, is there a way to make the folder display?
Thanks
<?php
$files = glob("{$_SERVER['DOCUMENT_ROOT']}/websites/MY 2006/*.*");
if(count($files) > 0)
{
echo "<h3>Backup Files for my.com 2006:</h3>\n<ul>\n";
foreach($files as $path)
{
$file = basename($path);
echo "<li><a href='http://my.com/websites/my 2006/$file'>$file</a></li>\n";
}
echo "</ul>\n";
}
?>
bokeh
07-07-2006, 03:05 AM
Like this:function ListChildDirectories($dir)
{
if ($handle = opendir($dir))
{
$output = array();
while (false !== ($item = readdir($handle)))
{
if (is_dir($dir.'/'.$item) and ($item != '.') and ($item != '..'))
{
$output[] = $item;
}
}
closedir($handle);
return $output;
}
else
{
return false;
}
}Or like this:function ListDescendantDirectories($dir)
{
if ($handle = opendir($dir))
{
$output = array();
while (false !== ($item = readdir($handle)))
{
if (is_dir($dir.'/'.$item) and $item != "." and $item != "..")
{
$output[] = $dir.'/'.$item;
$output = array_merge($output, ListDescendantDirectories($dir.'/'.$item));
}
}
closedir($handle);
return $output;
}
else
{
return false;
}
}
NogDog
07-07-2006, 10:08 AM
Just change the "*.*" to "*" and you'll get all file/directory names, not just those with a "." somewhere in them:
$files = glob("{$_SERVER['DOCUMENT_ROOT']}/websites/MY 2006/*");
comptech520
07-10-2006, 06:09 PM
When I do it this way with the * instead of *.* it pull the directories up but the other problem is when I click on the link for the directory, I get a forbidden error. Is there a way for when I click on the directory for it to open on the same page?
Thanks
NogDog
07-10-2006, 07:26 PM
Is there a default file in that directory (index.html, default.htm, etc.)? If not, depending on your webserver set-up, you won't be allowed to just link to and view the directory via the browser (done by FTP-ing that directory). You would have to instead link to a script with a URL query specifying what directory to view, and then have that script read the directory and display the results as in your script, above.