My next questions:
- If I have files and directories it lists both the same way. I would
like to add a class="dir" to the directory links so I can change the
look with CSS.
- The List shows ".", ".." and ".htaccess". How can I hide these?
<style type="text/css">
<!--
.file{
padding-left: 1em;
}
.dir{
padding-left: 0;
}
-->
</style>
<?php
$dir="";
if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
if($filename != "." && $filename != ".." && $filename
!= ".htaccess") // check for '.' '..' '.htaccess'
{
if(is_dir($dir.$filename))
$class="dir";
else
$class="file";
?>
<li class="<?php echo $class; ?>"><a
href="files/<?php echo $filename; ?>"><?php echo $filename;?></a></li>
<?php
}
}
closedir($dir_list);
}
?>
I was thinking about sorting the directory list alphabetically.
Surprisingly, there is no easy way to load a directory listing into an
array in PHP. Very disappointing.
As for this script: handling directories would need more fiddling.
Perhaps using a link such as ?dir=$dir.$filename for directories, then
making $dir=$_GET['dir'].
If you just want a simple clickable list of files it will probably be
easier to get your server to provide one.
The script I posted was a cut-down version of something else I was
working on recently to display pictures from a directory.