Directory listing.

A simple script to list the contents of a directory.

<?php
# Error Reporting
error_reporting(E_ERROR);
# Mirror Class
class mirror {
	public static $array;
	public static $array_dir;
	public static $handle;
	public static $home = '/home/example/public_html/';
	public static $url = 'http://example.co.uk/';
	
	public function getList($dir = '') {
		# Open Directory
		chdir(mirror::$home . $dir);
		mirror::$handle = opendir(mirror::$home . $dir);
		# Read Files
		while (false !== ($file = readdir(mirror::$handle))) {
			if ($file != "." && $file != ".." && $file != "bt" && $file != "kf" && $file != "index.ks" && $file != "index.html" && $file != "style.css") {
				if(is_dir($file)) {
					mirror::$array_dir[] = '<a href="http://example.co.uk/?dir=' . $dir . $file . '/">' . $file . '</a>';
				} else {
					mirror::$array[] = '<a href="'.mirror::$url.$dir.$file.'">' . $file . '</a>';
				}
			}
	    }
	    # Sort Array
	    sort(mirror::$array);
	    sort(mirror::$array_dir);
	    # Title
	    $content .= '<strong>Directories</strong><br />';
	    # Loop Through Each
	    foreach(mirror::$array_dir as $row) {
	    	$content .= $row . '<br />';
	    }
	    # Spacer
	    $content .= '<br />';
	    # Title
	    $content .= '<strong>Files</strong><br />';
	    foreach(mirror::$array as $row) {
	    	$content .= $row . '<br />';
	    }
	    # Return Content
	    return $content;
	}
}

$mirror = new mirror();

$content =  mirror::getList($_GET['dir']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<!-- Title -->
	<title>Directory Listing</title>
	<!-- Meta Tags -->
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	
</head>
<body>
	<?=$content;?>
</body>
</html>

Usage

Edit the necessary variables. The directory is to work from is obtained via $_GET['dir'] - and is relative to the root directory.


Comments

Add your comment