This code allows the administrator to set the root position for public file browsing on a server. Good for browsing libraries of files.
The attached file contains the images.
<?php
// This is highly stolen from Ash Young <ash@evoluted.net>. www.evoluted.net
// I rewrote it to do what I wanted it to do.
//Global Settings
$root = "/your/path/to/a/directory/" ; // this will the the root position of this script
$filetypes = array (
'png' => 'jpg.gif' ,
'jpeg' => 'jpg.gif' ,
'bmp' => 'jpg.gif' ,
'jpg' => 'jpg.gif' ,
'gif' => 'gif.gif' ,
'zip' => 'archive.png' ,
'rar' => 'archive.png' ,
'exe' => 'exe.gif' ,
'setup' => 'setup.gif' ,
'txt' => 'text.png' ,
'htm' => 'html.gif' ,
'html' => 'html.gif' ,
'fla' => 'fla.gif' ,
'swf' => 'swf.gif' ,
'xls' => 'xls.gif' ,
'doc' => 'doc.gif' ,
'sig' => 'sig.gif' ,
'fh10' => 'fh10.gif' ,
'pdf' => 'pdf.gif' ,
'psd' => 'psd.gif' ,
'rm' => 'real.gif' ,
'mpg' => 'video.gif' ,
'mpeg' => 'video.gif' ,
'mov' => 'video2.gif' ,
'avi' => 'video.gif' ,
'eps' => 'eps.gif' ,
'gz' => 'archive.png' ,
'asc' => 'sig.gif' ,
'wma' => 'audio.jpg' ,
'mp3' => 'audio.jpg' ,
);
//Set our root position and make sure the URL input is not manually manipulated
if (( substr ( $_GET [ 'dir' ], 0 , 2 ) != '/.' ) and ( substr ( $_GET [ 'dir' ], 0 , 1 ) != '.' ) and ( $_GET [ 'dir' ] != '' )) {
$mydir = $root . $_GET [ 'dir' ]; }
else {
$mydir = $root ;
}
//Get and sort the directory listing
$files = myscan ( $mydir );
sort ( $files );
//Compile our breadcrumb trail for the file paths
$breadcrumbs = "<a href='" . $_SERVER [ 'PHP_SELF' ] . "?dir='>Root</a>" ;
$bits = split ( '/' , $_GET [ 'dir' ]);
for ( $x = 1 ; $x < sizeof ( $bits ); $x ++) {
if ( $bits [ $x ] != ".." ) {
$sofar = $sofar . "/" . $bits [ $x ];
$breadcrumbs = $breadcrumbs . " > <a href='" . $_SERVER [ 'PHP_SELF' ] . "?dir=" . $sofar . "'>" . $bits [ $x ] . "</a>" ;
}
}
//Common functions used
function myscan ( $dir ) {
$arrfiles = array();
$arrfiles = opendir ( $dir );
while ( false !== ( $filename = readdir ( $arrfiles ))) {
$files [] = $filename ;
}
return $files ;
}
?>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Directory Listing of Root<?=$sofar ; ?> </title>
<link rel="stylesheet" type="text/css" href="dlf/styles.css" />
</head>
<body>
<div id="container">
<h1>Directory Listing of Root<?=$sofar ; ?> </h1>
<div id="breadcrumbs"> <? echo $breadcrumbs ; ?> </div>
<div id="listingcontainer">
<div id="listingheader">
<div id="headerfile">File</div>
<div id="headersize">Size</div>
<div id="headermodified">Last Modified</div>
</div>
<div id="listing">
<?
for ( $x = 0 ; $x < sizeof ( $files ); $x ++) {
if (( $files [ $x ] != '.' ) and ( $files [ $x ] != ".." )) {
echo "<div>" ;
if( is_dir ( $mydir . "/" . $files [ $x ])) {
echo "<a href='" . $_SERVER [ 'PHP_SELF' ] . "?dir=" . $sofar . "/" . $files [ $x ] . "' class='g'><img
src='dlf/folder.png'><strong>" . $files [ $x ] . "</strong>" ;
}
else {
$ext = strtolower ( substr ( $files [ $x ], strrpos ( $files [ $x ], '.' )+ 1 ));
if( $filetypes [ $ext ]) {
$icon = $filetypes [ $ext ];
} else {
$icon = 'unknown.png' ;
}
if ( strlen ( $files [ $x ]) > 70 ) {
echo "<a class='g'><img src='dlf/" . $icon . "'<strong>" . substr ( $files [ $x ], 0 , 70 ) . "...</strong>" ; }
else {
echo "<a class='g'><img src='dlf/" . $icon . "'<strong>" . $files [ $x ] . "</strong>" ;
}
}
echo "<em>" . round ( filesize ( $mydir . "/" . $files [ $x ])/ 1024 ) . " KB</em>" ;
echo date ( "M d Y h:i:s A" , filemtime ( $mydir . "/" . $files [ $x ]));
echo "</a></div>" ;
}
}
?>
</div>
</div>
</div>
</body>
</HTML>
List the content of the directory of your webserver where this small PHP Script resides. Categories : PHP , Filesystem , Directories , CSS Directory TreeView - File Manager & Explorer - FTP - Utility - PHP/HTML - Categories : PHP , Directories , FTP , Filesystem , HTML and PHP a file explorer for the web, filesystem php php3 files dirs directories pictures files windows linux system list ls scripts Categories : PHP , URLs , Directories , Filesystem grab directory listings into an array the example prints out each
subdirectory in the main dir - further work is to be performed on this one Categories : Filesystem , PHP , Directories , Search , Utilities Listing the 10 most recently updated files in a given dir by using last-
modified variable and printing to html with link to the file Categories : PHP , Directories , Filesystem getDirArray(Path,Filter,Sorted): Returns an array of the files in a directory,
filtered by regular expression and either sorted or randomized. Good for
random pictures and graphics. Categories : PHP , Filesystem , Directories Extended Get File List Function Categories : PHP , Filesystem , Search , Directories Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem , Directories , Arrays , PHP PHP4 DirectoryIterator Class Categories : PHP , PHP Classes , Filesystem , Directories A function which places the path and name of all subdirectories into an array Categories : PHP , Filesystem , Arrays , Directories Using PHP to Delete a directory with all sub directories and files using FTP Categories : PHP , FTP , Directories , Filesystem Directory Viewer, Directory Content Viewer, Directory Structure to HTML.
This code will basically create a complete set of HTMLs to let a user
navigate through any directory you want. Excellent code for large file
sharing pages. Categories : Directories , Filesystem , PHP Directory viewer, customize how you display the file structure, easy to
understand. Found out about PHP 3 days ago, and this is my first prog. Categories : HTML and PHP , Complete Programs , Directories , Filesystem , PHP Open directory and File download Categories : PHP , Filesystem , Directories , HTML and PHP filesplit : Split big text files in multiple small ones Categories : PHP , Log Files , Filesystem , PHP Classes
Philip Bechtel wrote : 1628
Hello,
Thanks for your example! I found a missing component from the example
"PHP Directory Listing" however.
The code below fails to produce a correct link. It begins the link with
"<a" and then fails to produce any "href=url" at all. For example:
"<a class=`g`><img src=`dlf/jpg.gif`><strong>pict0256.jpg</strong><em>201
KB</em>Nov 30 2005 05:45:00 PM</a>"
The failed code is listed below:
--------------
if (strlen($files[$x]) > 70) {
echo "<a class=`g`><img src=`dlf/" . $icon . strong>" .
substr($files[$x],0,70) . "...</strong>"; }
else {
echo "<a class=`g`><img src=`dlf/" .
$icon .
"`<strong>" . $files[$x] . "</strong>"; }
--------------
Thanks Again!
Phil