|
/////////////////////////////////////////////////////////////
//
// PHP Directory Listing Script
//
/////////////////////////////////////////////////////////////
//
// This script lists the files in a directory, displays
// directory and filesize information, as well as adding
// icons to indicate differing filetypes.
//
// Original Author: d_fens - www.redbrick.dcu.ie/~dfens
// Modified By: CuLT - www.redbrick.dcu.ie/~cult
// Modifications:
// * Reduced execution time
// * Added getFileName() function
// * Greatly improved indentation :P
// * Clarified comments and added new ones
// * Added byte conversion section
// * Filesize virtually unchanged
// * Redesigned as an include but can still
// run standalone.
//
// Modified with permission from author.
// Last Modified: 27th May 2005
// Copyright (c) 2005 by d_fens
//
/////////////////////////////////////////////////////////////
?>
hostname:
echo ''.$_SERVER['HTTP_HOST'].''; //prints the host address to the page
?>
the absolute path to this folder is:
$path = $_SERVER['SCRIPT_FILENAME']; //Gives the full path and file name or script eg. /home/members/.../name.php
$position = strrpos($path,'/'); //Gives the point for the last "/" up to but not including the "/"
$path = substr($path,0,$position+1); //Gives the URL from 0 up to $position+1 which adds the "/"
echo $path;
echo ' ';
echo substr(__FILE__,0,strrpos(__FILE__,'/')+1); //A substring of the file which is /webtree/...filename, from the first letter up to the "/" including it, FILE is whatever its called from, including includes or function files
?>
current filename:
$filename = getFileName();
echo ''.$filename.''; //prints the filename as a link to the file on the server
?>
//the following retrieves the file information from the folder and stores it in arrays
$filelength = strlen(getFileName());
$address = substr($_SERVER['PHP_SELF'], 0, -$filelength);
$host = $_SERVER['HTTP_HOST'];
$files = array();
$filesizes = array();
$dir = opendir('.');
while(($file = readdir($dir)) !== false){
if($file !== '.' && $file !== '..' && !is_dir($file)){
$files[] = $file;
$filesizes[] = filesize($file);
}
}
closedir($dir);
?>
this folder contains the files:
//this begins the loop which prints out the filenames and attaches the relevant filesize to each title tag
$totalsize = 0;
for($i=0; $i ';
else if($ext=='gif' || $ext =='jpg' || $ext =='jpeg' || $ext =='png') echo ' ';
else if($ext=='txt' || $ext =='css') echo ' ';
else if($ext=='zip' || $ext =='rar') echo ' ';
else if($ext=='exe' || $ext =='bat') echo ' ';
else echo ' ';
echo ''.$filename.' ';
echo "\n";
}
// the following determines how to display the folder size information
if(strlen($totalsize)>3 && strlen($totalsize)<7){
$finalfilesize = substr(($totalsize / 1024),(strrpos($totalsize,'.')),strlen($totalsize)); //converts bytes to kilobytes
$bytesize = 'KBytes';
}
else if(strlen($totalsize)>6 && strlen($totalsize)<10){
$finalfilesize = substr(($totalsize / 1048576),(strrpos($totalsize,'.')),strlen($totalsize)-3); //converts bytes to megabytes
$bytesize = 'MBytes';
}
else if(strlen($totalsize)>9){
$finalfilesize = substr(($totalsize / 1073741824),(strrpos($totalsize,'.')),strlen($totalsize)-6); //converts bytes to gigabytes
$bytesize = 'GBytes';
}
else{
$finalfilesize = $totalsize; //if, it's not kb, mb or gb, it'll just have to be bytes!
$bytesize = 'bytes';
}
echo ' folder size: '.$finalfilesize.' '.$bytesize.'';
echo ' ';
//script functions, the names should make each one self explanatory
function getFileExtension ( $name ){
$position = strrpos($name,'.');
if ($position === false) {
return '';
} else return strtolower(substr($name,$position + 1));
}
function removeFileExtension ( $name ){
$position = strrpos($name,'.');
if ($position === false){
return $name;
} return substr($name,0,$position);
}
function getFileName (){ //Gives the full path and file name or script eg. /home/members/.../name.php strips it and keeps the "/" BUT strips it from the "/" ONWARDS
return substr($_SERVER['SCRIPT_FILENAME'],strrpos($_SERVER['SCRIPT_FILENAME'],'/')+1);
}
?>
|