Forum Moderators: coopster

Message Too Old, No Replies

Directory View

         

branmh

7:14 pm on Sep 1, 2003 (gmt 0)

10+ Year Member



How would I create a small php script that will show the current directories files and sub-directories, and have the files clickable?

coopster

9:52 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can start with this (a stripped down version of a similar function I once wrote), it shows files in a specific directory. You'll have to add your own subdirectory routine processing:

<html><head><title>Directory Listing</title></head><body><table>
<?php
function file_size_info($filesize) {
$bytes = array('KB', 'KB', 'MB', 'GB', 'TB'); # values displayed in at least kilobytes.
if ($filesize < 1024) $filesize = 1;
for ($i = 0; $filesize > 1024; $i++) $filesize /= 1024;
$file_size_info['size'] = ceil($filesize);
$file_size_info['type'] = $bytes[$i];
return $file_size_info;
}
$dir = '/path/to/your/directory/';
$pathHttp = '/path/to/the/directory/url/';
if ($handle = @opendir($dir)) {
while (false!== ($filename = readdir($handle))) {
$files[] = $filename;
}
closedir($handle);
natcasesort($files);
foreach ($files as $filename) {
if (is_file($dir.$filename) and substr($filename, 0 , 1)!= '.') {
$file_size_info = file_size_info(filesize($dir.$filename));?>
<tr>
<td>
<a href="<?php print $pathHttp.$filename;?>"><?php print $filename;?></a>
</td>
<td align="right"><?php print $file_size_info['size'];?></td>
<td align="right"><?php print $file_size_info['type'];?></td>
<td><?php print date("Y-m-d", filemtime($dir.$filename));?>&nbsp;</td>
<td align="right"><?php print date("H:i:s", filemtime($dir.$filename));?></td>
</tr>
<?php
}}
unset($files);
}
?>
</table></body></html>