rrinc -
Joined: 24 Feb 2006 Posts: 725 Location: Arkansas, USA
|
Posted: Sat Aug 04, 2007 5:28 pm Post subject: My PHP Directory Listing Script |
|
|
I thought I would post my directory listing script, it isn't sortable, but it pretty simple and provides file sizes and distinctions between files/folders.
Code: | <?php /* Directory Listing */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo str_replace('http://','',''.$_SERVER['SCRIPT_URI']); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<style type="text/css">
a:link, a:visited, a:active {color:red;}
body {font-size:90%;font-family:verdana; }
</style>
</head>
<body>
<!-- HEADER HTML -->
<span style="font-size: 175%;"><?php echo str_replace('http://','',''.$_SERVER['SCRIPT_URI']); ?></span><br /><hr />
<table border=0 style="font-size:90%;">
<tr><td><u><b>File</b></u></td><td><u><b>Size</b></u></td><td><u><b>Type</b></u></td></tr>
<!-- END OF HEADER HTML -->
<!-- DIRECTORY LISTING: -->
<?php
strpos(strtolower(PHP_OS),'win') ? $path = $_SERVER['DOCUMENT_ROOT'].str_replace('index.php','',str_replace('/','\\',$_SERVER["REQUEST_URI"])) :
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$dir_handle = @opendir($path) or die("Cannot read folder contents.<br>");
while ($file = readdir($dir_handle))
{
if($file!='.' && $file!='..' && $file!='index.php')
{
if (is_file($file)) { $type = 'File'; }
else { $type = 'Folder'; }
if ($type == 'File')
{
$size = filesize($file);
if ($size >= 1024)
{$size = $size / 1024; $unit = 'KB';}
if ($size >= 1024)
{$size = $size / 1024; $unit = 'MB';}
$size = round($size).' '.$unit;
}
echo "<tr><td style=\"padding-right:15px;\"><a href=\"$file\">$file</a></td><td>$size</td><td>$type</td></tr>";
}
}
closedir($dir_handle);
?></table>
<!-- END OF DIRECTORY LISTING -->
<!-- FOOTER HTML -->
<hr />
</body>
</html> |
It's nothing awesome, but I made it awhile back when playing with directories and files. I might make a better version some time.
Do you think I chose a good way to get the folder's path? It should work on Windows or Linux (if you notice, I have a small check for the OS...I haven't tested it yet). _________________ -Blake | New Server :D
SaveTheInternet
Soy hispanohablante. Puedes contactarme por mensajes privados. |
|