This message was edited by codecraig at 2004-1-14 4:26:42
here's the code, you can try it yourself
images can downloaded from:
www.codecraig.com/images/text.gif
www.codecraig.com/images/textfolder.gif
www.codecraig.com/images/folder.gif
<?php
/**
* Author: Craig Wickesser <craig@codecraig.com>
* Date: Jan. 10, 2004
*
* Prerequisites:
* 1. PHP 4.3+
* 2. directory named images with the following images:
* text.gif
* folder.gif
* textfolder.gif
* 3. The supplied path must be a directory that is a in the same
* directory as this script.
* Example:
* /www/testDir
* /www/showDirsAndFiles.php
* path = testDir/
*
* Your final directory structure should be:
* /your_path/showDirsAndFiles.php
* /your_path/images/
* /your_path/images/text.gif
* /your_path/images/folder.gif
* /your_path/images/textfolder.gif
*
* OPTIONS:
* Turn off details: In the showIt() method change the
* $showDetails variable to false
**/
if((isset($_GET[path]))&&($_GET[path] != "")) {
// Get the path and display folders and files
$path = $_GET[path];
// to stop people from looking at to much :) (not the best method, but good 4 now)
if(($path == ".")||($path == "..")||($path == "./")||($path == "/..")||($path == "./..")||($path == "../")||($path == "../../")||($path == "../..")||($path == "/")||($path == ""))
showError();
else {
$error = false;
$pathPieces = explode("/", $_SERVER['SCRIPT_NAME']);
$images = "/" . $pathPieces[1] . "/" . $images;
if(count(explode("/", $path)) == 1)
$path = $path . "/";
showIt($path);
}
}
else {
// No path was given. Display error message and form to get path.
showError();
}
/**
* Displays folders and files given a path.
* @param $path
**/
function showIt($path) {
// initialize variables
$directories = array();
$files = array();
$dirPath = array();
$filePath = array();
$count = -1;
$fileAndDirCounter = 0;
$showDetails = true;
// open directory handle
if(is_dir($path))
{
if($dir = opendir($path))
{
$pathPieces = explode("/", $path);
// Print heading
// If path is just <directory_name>/
if($pathPieces[1] == "")
{
print "<img src=\"images\\textfolder.gif\"> ";
print("<B>$pathPieces[0]</b><hr width=40% align=left>");
}
else
{
foreach($pathPieces as $v)
{
$count++;
}
$count--;
print "<img src=\"images\\textfolder.gif\"> ";
print("<B>$pathPieces[$count]</b><hr width=40% align=left>");
}
// spin through files/directories in $dir
while ($file = readdir($dir))
{
// first two files/directories are always . and .. so don't count them
if($fileAndDirCounter > 1)
{
// this should be <directory_name>/someFile.txt
// OR <directory_name>/<directory_name>
$fullPath = $path . $file;
if(is_dir($fullPath))
{
// Keep track of the directories and their paths
$dirPath[$fileAndDirCounter] = $fullPath . "/";
$directories[$fileAndDirCounter] = $file;
}
else
{
// Keep track of the files and their paths
$filePath[$fileAndDirCounter] = $fullPath;
$files[$fileAndDirCounter] = $file;
} // end else
} // end if
$fileAndDirCounter++;
} // end while loop
// Close handle to directory
closedir($dir);
// 1. Sort the array of directories
// 2. Print the directories with folder icons
print "<table>";
if(count($directories) != 0) {
natcasesort($directories);
reset($directories);
$counter = count($directories) + 2;
$tempCounter = 2;
$newArray = array_merge($dirPath,$directories);
$half = count($newArray) / 2;
$y = 0;
for($x=0; $x < $half ;$x++) {
$t = $half + $y;
print "<tr>";
print "<td>";
print " <img src=\"images\folder.gif\"> ";
print "<a href=\"showDirsAndFiles.php?path=$newArray[$x]\">$newArray[$t]</a>";
print "</td>";
print "</tr>";
$y++;
}
}
print "</table>";
// 1. Sort the array of files
// 2. Print the files with text icons
print "<table>";
if(count($files) != 0) {
natcasesort($files);
reset($files);
$counter = count($files) + 2;
$tempCounter = 2;
$newArray = array_merge($filePath,$files);
$half = count($newArray) / 2;
$y = 0;
for($x=0; $x < $half ;$x++) {
$t = $half + $y;
print "<tr>";
print "<td>";
print " <img src=images\\text.gif> ";
print "<a href=\"$newArray[$x]\">$newArray[$t]</a>";
print "</td>";
if($showDetails)
{
print "<td align=left>";
print "<font size=2>";
print " ";
print "Size: " . fsize($newArray[$x]);
print "</font>";
print "</td>";
print "<td>";
print " ";
print "<font size=2>";
print "Modified: " . date ("n/j/Y g:i a", filemtime($newArray[$x])) . "";
print "</font>";
print "</td>";
}
print "</tr>";
$y++;
}
}
print "</table>";
}
else
{
print "<font color=red><B>Error</B></font>";
print " ";
print "Could not open directory handle to <b>$path</b>";
print "";
$error = true;
}
}
else
{
print "<font color=red><B>Error</B></font>";
print " ";
print "You entered an invalid directory: <b>$path</b>";
print "";
$error = true;
}
}
if(!$error) {
print "";
print "<a href=\"javascript:history.go(-1)\">Go Back</a>";
}
// This function gets the file size and displays label
function fsize($file) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");
$pos = 0;
$size = filesize($file);
while ($size >= 1024) {
$size /= 1024;
$pos++;
}
return round($size,2)." ".$a[$pos];
}
function showError() {
// No path was given. Display error message and form to get path.
print "<font color=red><B>Error</B></font>";
print " ";
print "No path was given. Please provide a path in the following format: <directory_name>/";
print "";
print " ";
print "<B>Example:</B> test/";
print "";
print "<form name=frmNeedPath action=showDirsAndFiles.php method=get>";
print "Path: ";
print "<input type=text length=15 name=path>";
print "\t";
print "<input type=submit value=submit>";
print "</form>";
$error = true;
}
?>
Code:Craig