-
Feb 29, 2008
Custom Directory Listing
The following code will give you an XHTML validating directory listing with icons for filenames that are easily customizable and easy to add more file types. You just need to come up with your own icons. An example can be found here and the full source code can be found here.
First, in the body of your XHTML document declare some variables and then read the files from the directory in which this file is in. Also I am going to declare a function in this file for the tutorial. Just to make it easier to show, I would recommend putting it in a functions file and then including it into this file.
Note: To have this come up automatically just name this file index.php and put it in whichever directory you want.
<?php
/*This file takes the filesize of something and converts it into something a little more readable*/
function have_filesize($num) {
$size = $bytes / 1024;
if($size < 1024) {
$size = number_format($size, 2);
$size .= ' KB';
} else {
if($size / 1024 < 1024) {
$size = number_format($size / 1024, 2);
$size .= ' MB';
} else if ($size / 1024 / 1024 < 1024) {
$size = number_format($size / 1024 / 1024, 2);
$size .= ' GB';
}
}
return $size;
}
$cellcolor2 = "alt"; //Declare alternate list item class
$dir = ".";
$open = opendir ($dir); //Open current directory
?>Next you have to loop through all of the files in the directory and to do that you simply do this.
Inside of that while loop is where all of the magic happens<?php
while ($Files = readdir ($Open)) {
$Filename = "$Dir/" . $Files;
$Type = filetype ("$Filename");
if ($Files == '..'|| $Files == '.' || $Files == 'index.php') {
continue;
} else {
//If the file is a directory, list it out with a folder icon
if (is_dir ($Filename)) {
$Name = "<a href="".$Filename.""><img src="/i/folder.png" alt="Directory" />".$Files."</a>";
$Size = "Directory"; //Set Size column to "Directory"
} else {
//This grabs whatever is after the last . in the filename. This is good for files that have multiple periods in it's name
$last = substr(strrchr($Files, "."), 1);
//Create a list of file types to associate with icons
$types = array("jpg", "jpeg", "gif", "png", "php", "mp3", "zip", "psd", "pdf");
//If the filetype of the file in the directory matches one in the types array above then set an icon for it
if(in_array($last, $types)) {
switch($last) {
case "jpg":
case "jpeg":
case "gif":
case "pdf":
case "png": $image = "/i/image.png"; break;
case "psd": $image = "/i/psd.png"; break;
case "php": $image = "/i/php.png"; break;
case "mp3": $image = "/i/mp3.png"; break;
case "zip": $image = "/i/zip.png"; break;
default: $image = "/i/default.png"; break;
}
} else {
//Set a default icon for the filetype
$image = "/i/default.png";
}
//Create the link to the file with the icon
$Name = "<a href="".$Filename.""><img src="".$image."" alt="".$last." File" />".$Files."</a>";
//Set the filesize of the file
$filesize = filesize ($Filename);
//Create friendly filesize
$Size = have_filesize($filesize);
}
}
//Set name, size and type arrays
$FileArray[] = $Name;
$SizeArray[] = $Size;
$TypeArray[] = $Type;
}
//Close directory
closedir ($Open);
?>Lastly this is where the sorting is handled and everything gets printed out.
<?php
// Define the two ways to sort the directory contents
$Sort = $_GET['sort'];
switch ($Sort) {
case "SortByLow":
array_multisort ($SizeArray, SORT_ASC, $FileArray);
$sorting = "?sort=SortByHigh";
break;
case "SortByHigh":
array_multisort ($SizeArray, SORT_DESC, $FileArray);
$sorting = "?sort=SortByLow";
break;
default:
array_multisort ($TypeArray, $FileArray, $SizeArray);
$sorting = "?sort=SortByLow";
break;
}
$cl = "";
// Print out the contents of the directory
echo "<ul>n";
echo "<li><span><a href="".$sorting."">Size</a></span><a href="".$_SERVER['PHP_SELF']."">Filename</a></li>n";
for ($n = 0; $n < count($FileArray); $n++) {
//This is used to place a class in every other list item for a different colored background
if($num = is_float($n/2)) {
$cl = " class="$cellcolor2"";
} else { $cl=""; }
if($SizeArray[$n] == "directory") {
echo ("<li".$cl."><span>$SizeArray[$n]</span>$FileArray[$n]</li>n");
} else {
echo ("<li".$cl."><span>$SizeArray[$n] kb</span>$FileArray[$n]</li>n");
}
}
echo "</ul>";
?>Posted by Mark on 02/29/2008 12:33 PM in PHP Tutorials
Comments
haveboard
02/29/2008
File naming convention infiltration, FTW!