Page 1 of 1

php: Directory viewer

Posted: Thu Oct 16, 2003 4:13 am
by Deimos
Here's a script for viewing the files and dirs in the directory this file is placed in.. I couldn't post it at one time, so I devided it in 2 halfs. You should paste the 2 parts together to make it work. You can add a style sheet to adjust the design.



[php]<?php



$scriptLocation = ""; // Point this to a refernce to a style sheet (Embeeded CSS will be ingnorned)



$scriptCSS = "



";

/*

** CSS (Style Sheet) Definition Finished

*/





/*

** Display Disk Space Usagae and Free

*/

$scriptStats = "1"; // 1 for Yes - 0 for No









/*

*********************** DO NOT MODIFY BELOW THIS LINE ***********************

*/

function dirHeader() {

$content = "<table width=100% nowrap>";

return $content;

}



function dirTable() {

$content = "<tr><td><b><font size=-1>Type</font></b></td><td width=50%><b><font size=-1>Name</font></b></td><td><b><font size=-1>Size</font></b></td><td><b><font size=-1>Modified</font></b></td></tr>";

return $content;

}





function dirFooter() {

$content = "</table>";

return $content;

}



function fType($file) {

$varFileType = filetype($file);

if($varFileType != "dir") {

$curdir = getcwd();

$pInfo = pathinfo("$curdir/$file");

$varFileType = $pInfo["extension"];

}

return $varFileType;

}





function fileView($file) {

$varType = strtolower(fType($file));

$varJSSettings = "width=300,height=300,resizable=1,scrollbars=1,menubar=0,status=0,titlebar=0,toolbar=0,hotkeys=0,locationbar=0";

$txtArray[] = "txt";

$txtArray[] = "nfo";

$txtArray[] = "diz";

$txtArray[] = "now";

$txtArray[] = "bmp";

$txtArray[] = "jpg";

$txtArray[] = "gif";

$txtArray[] = "doc";

$txtArray[] = "1st";

$txtArray[] = "now";

$txtArray[] = "me";

if(in_array($varType, $txtArray)) {

$content = " - (<a href=\"#\" onClick=\"window.open('$file', 'viewer','$varJSSettings');\">view</a>)";

}

return $content;

}

function display_size($file_size){

if($file_size >= 1073741824) {

$file_size = round($file_size / 1073741824 * 100) / 100 . "g";

} elseif($file_size >= 1048576) {

$file_size = round($file_size / 1048576 * 100) / 100 . "m";

} elseif($file_size >= 1024) {

$file_size = round($file_size / 1024 * 100) / 100 . "k";

} else {

$file_size = $file_size . "b";

}

return $file_size;

}





?>//Forget this line it's not needed when you past part 2 here

[/php]

Posted: Thu Oct 16, 2003 4:39 am
by Deimos
[php]

//Part 2



<? //forget this one in the script you make, it's not needed.

function dirGather() {

$handle=opendir(".");

$content = "";

//while (false!=($file = readdir($handle))) {

while ($file = readdir($handle)) {

if(($file != "index.txt") && ($file != "index.php")) {

$filetype = fType($file);

if($filetype == "dir") {

$dirtext[] = "$file";

} else {

$context[] = "$file";

}

}

}

if($dirtext) {

sort($dirtext);

for($i=0; $i<count($dirtext); $i++) {

$file = $dirtext[$i];

$lastchanged = filectime($file);

$changeddate = date("d-m-Y H:i:s", $lastchanged);

$filesize = display_size(filesize($file));

$filetype = fType($file);

$viewfile = fileView($file);

$content .= "<tr><td><font size=-1>$filetype</font></td>";

$content .= "<td><font size=-1><a href=\"$file\">$file</a> $viewfile</font></td>";

$content .= "<td><font size=-1>$filesize</font></td>";

$content .= "<td><font size=-1>$changeddate</font></td></tr>";

}

}

if($context) {

sort($context);

for($i=0; $i<count($context); $i++) {

$file = $context[$i];

$lastchanged = filectime($file);

$changeddate = date("d-m-Y H:i:s", $lastchanged);

$filesize = display_size(filesize($file));

$filetype = fType($file);

$viewfile = fileView($file);

$content .= "<tr><td><font size=-1>$filetype</font></td>";

$content .= "<td><font size=-1><a href=\"$file\">$file</a> $viewfile</font></td>";

$content .= "<td><font size=-1>$filesize</font></td>";

$content .= "<td><font size=-1>$changeddate</font></td></tr>";

}

}

return $content;

}



function diskStats($scriptStats) {

if($scriptStats) {

// $diskTotal = display_size(disk_total_space("/"));

$diskFree = display_size(diskfreespace("/"));

$content = "<table width=100%>";

$content .= "<tr><td width=150><b><font size=-1>Free Disk Space:</font></b></td><td><font size=-1>$diskFree</font></td></tr>";

// $content .= "<tr><td width=150><b><font size=-1>Total Disk Space:</font></b></td><td><font size=-1>$diskFree</font></td></tr>";

$content .= "</table>";

print($content);

}

}

?>

<html>

<head>



<? if($scriptLocation == "") {

print($scriptCSS);

} else {

print("<LINK REL=stylesheet HREF=\"$scriptLocation\" TYPE=\"text/css\">");

} ?>

</head>

<body>

<?



diskStats($scriptStats);

print(dirHeader());

print(dirTable());

print(dirGather());

print(dirFooter());



?>



//End of part 2

[/php]