10-07-2008, 04:30
|
#1 (permalink)
|
|
Warchief
Join Date: Apr 2003
Posts: 213
|
php directory listing
I'm working on a simple php web based directory listing script. Currently it has the ability to list files and folders - download the file or move to another directory. When I move to another directory it lists the filename but I get and error 'Warning: filesize(): stat failed' on the filetype and filesize - I think this is because it's trying to read that info from the base directory rather than the new directory - any help would be appreciated. Here's the code:
Code:
$sub = ($_GET['dir']);
$path = dirname($_SERVER['SCRIPT_FILENAME']);
$path = $path . "$sub";
$myDirectory = opendir($path);
// get each entry
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
// close directory
// count elements in array
$indexCount = count($dirArray);
// sort 'em
sort($dirArray);
function size_hum_read($size){
// Adapted from: http://www.php.net/manual/en/function.filesize.php
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}
echo $sub;
// print 'em
print("<TABLE align=center width=750 border=1 bordercolor=#c7db44 cellpadding=5 cellspacing=0>\n");
print("<TR bgcolor=#c7db44 class=main_type1><TH>Name</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
if ((substr("$dirArray[$index]", 0, 1) != ".") && (substr("$dirArray[$index]", 0, 1) != "..") && (substr("$dirArray[$index]",-4) != ".php")) {
if (substr("$dirArray[$index]", -4, 1) == "."){
print("<TR><TD><a href=\"$filepath$sub/$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(filetype($dirArray[$index]));
print("</td>");
print("<td>");
print(size_hum_read(filesize($dirArray[$index])));
print("</td>");
print("</TR>\n");
} else {
print("<TR><TD><a href=\"?dir=$sub/$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(filetype($dirArray[$index]));
print("</td>");
print("<td>");
print(size_hum_read(filesize($dirArray[$index])));
print("</td>");
print("</TR>\n");
}
}
}
print("</TABLE>\n");
closedir($myDirectory);
|
|
|
|