DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Drop-In PHP Script For Listing Files In A Directory + Optional Passphrase Protection

06.12.2009
| 6276 views |
  • submit to reddit
        This script can be dropped into a directory on a web server (e.g. as index.php) to provide a very basic, nice-looking directory listing of that directory.  It is made specifically to operate on the directory it is in, so it doesn't apply to subdirectories.  Additionally, you can password-protect the listing by modifying the settings near the top of the script.

The security of the password protection is probably questionable and indicative of my newness to PHP, so I wouldn't recommend using it to protect highly sensitive files.  Also, be sure any file/directory permissions are set accordingly.

Icons are borrowed from Wikimedia Commons.  Many file types aren't represented (because I'm lazy :P).

<?php
	session_start();
	
	/* SETTINGS */
	
	// Set this to true if you want to password-protect this directory
	$protected = false;

	// Passphrase for this directory, if protected
	$passphrase = "";



	/* SCRIPT LOGIC */

	// If they've logged out
	if ( isset($_REQUEST['logout']) )
		unset($_SESSION[$SESSION_ID]);

	// If no authorization is even needed on this page
	if ( $protected == false )
		$auth = "good";

	// If session id is already set
	else if ( $_SESSION[$SESSION_ID] == $passphrase )
		$auth = "good";

	// Else, if a password was given
	else if ( isset($_POST['passphrase']) )
		if ( $_POST['passphrase'] == $passphrase )
		{
			$auth = "good";
			$_SESSION[$SESSION_ID] = $passphrase;
		}
		else $auth = "wrong";

	// If there's no active session and no given password
	else $auth = "none";

?>

<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Directory Listing</title>
<link rel="icon" type="image/png" href="http://upload.wikimedia.org/wikipedia/commons/3/39/Folder-silk.png">
<style>
body {
	background: #888;
	font-family: Georgia;
	font-size: small;
}
a:link, a:visited {
        color: #111;
        text-decoration: none;
        border-bottom: 1px dotted;
}
a:hover, a:active {
        color: #444;
        border-bottom: 1px solid;
}
#container {
	width: 500px;
	margin: auto;
}
#content {
	background-color: #eee;
	background-image: url(/images/bodygrad.png);
	background-repeat: repeat-x;
	background-position: top left;
	border: 10px #999  solid;
	margin-top: 50px;
	min-height: 25px;
	padding: 25px;
}
#content h1, #content h2 {
	font-weight: normal;
	margin: 0;
}
#content h1 {
	color: #222;
	margin-bottom: 25px;
	font-size: 28px;
	border-bottom: 1px dashed black;
}
#content h2 {
	color: #333;
	margin-bottom: 25px;
	font-size: 18px;
}
#footer {
	color: #555;
	font-size: x-small;
	text-align: center;
}
input {
	font-size: 20px;
	padding: 5px 5px;
	margin-bottom: 5px;
	border: 2px #999 solid;
	background-color: #f0f0f0;
}
input:hover, input:active {
	background-color: white;
}
input.button {
	font-size: 16px;
	font-family: Georgia;
	padding: 2px 20px;
}
input.wide {
	width: 300px;
}
input.halfwide {
	width: 150px;
}
.message {
	background-repeat: no-repeat;  
	background-position: 17px center;
	border: 1px;
	border-style: solid;
	margin: 10px -10px;
	margin-bottom: 20px;
	padding: 10px 50px;
	width: 290px;
}
.notice {
	background-color: #ccc;
	background-image: url(http://upload.wikimedia.org/wikipedia/commons/c/cc/Note.png);
	border-color: #888;
}
.warning {
	background-color: #ff9;
	background-image: url(http://upload.wikimedia.org/wikipedia/commons/4/49/Error.png);
	border-color: #ee0;
}
.error {
	background-color: #fcc;
	background-image: url(http://upload.wikimedia.org/wikipedia/commons/c/c0/Exclamation.png);
	border-color: #e00;
}
.success {
	background-color: #9fa;
	background-image: url(http://upload.wikimedia.org/wikipedia/commons/2/22/Accept.png);
	border-color: #0c2;
}
</style>
</head>

<body>

<div id="container">

<div id="content">

<?php
	if ( $auth == "wrong" ) {
		echo '<div class="error message">Incorrect passphrase.</div>';
	}

	if ( $auth == "none" || $auth == "wrong" ) {
		echo '<div class="promptbox">Please enter the passphrase to continue.</div>';
		echo <<< EOT

		<form method="post" action="">
			<input type="password" name="passphrase" \>
			<input class="button" type=submit name="Submit" value="Submit" />
		</form>

EOT;
	}

	if ( $auth == "good" ) {

		// Define the path as being the current directory
		$path = "./";

		// Open the current directory
		$dir_handle = @opendir($path) or die("Unable to open $path");

		echo "<h1>Directory Listing</h1>\n";

		// Loop through the directory, sticking filenames into $files[]
		while ($file = readdir($dir_handle)) {
			if ( $file[0] != '.' && stripos($file,"php") == FALSE )
				$files[] = $file;
		}

		// Closing the directory
		closedir($dir_handle);
		
		// If the directory is empty
		if ( count($files) == 0 ) {

			echo '<div class="notice message">Directory is empty.</div>';

		}
		else {
		
			// sort the array
			sort($files);
			
			foreach ( $files as $file ) {

				$pathinfo = pathinfo($file);
				$ext = strtolower($pathinfo['extension']);

				switch ($ext) {

					case "jpg":
						$icon = "http://upload.wikimedia.org/wikipedia/commons/6/65/Image-silk.png";
						break;

					case "png":
					case "gif":
						$icon = "http://upload.wikimedia.org/wikipedia/commons/4/4c/Page_white_paintbrush.png";
						break;

					case "htm":
					case "html":
						$icon = "http://upload.wikimedia.org/wikipedia/commons/7/7b/Page_white_world.png";
						break;
					
					case "doc":
					case "docx":
						$icon = "http://upload.wikimedia.org/wikipedia/commons/3/32/Page_word.png";
						$icon = "http://upload.wikimedia.org/wikipedia/commons/a/a1/Page_white_word.png";
						break;
						
					case "xls":
					case "xlsx":	
						$icon = "http://upload.wikimedia.org/wikipedia/commons/9/9c/Page_excel.png";
						break;
					
					case "pdf":
						$icon = "http://upload.wikimedia.org/wikipedia/commons/1/11/Page_white_acrobat.png";
						break;
					
					case "zip":
					case "tar":
					case "jar":
					case "sit":
					case "gz":
					case "bz2":
					case "7z":
						$icon = "http://upload.wikimedia.org/wikipedia/commons/d/d2/Page_white_zip.png";
						break;

					default:
						$icon = "http://upload.wikimedia.org/wikipedia/commons/7/75/Page_white.png";
				}
				
				echo "<img style='width:16px; height:16px; margin-right:.5em;' alt='$ext' src='$icon'><a href='$file'>$file</a><br/>\n";
				
			}	// end foreach
			
		}	// end else

		echo '<div style="margin-top:25px; display:block;"><a href="./index.php?logout=yes">Sign Out</a></div>';

	}

?>

</div>
<div id="footer">
	<p>Icons provided by the <a href='http://www.famfamfam.com/lab/icons/silk/'>Silk icon set</a> and hosted at <a href='http://commons.wikimedia.org/wiki/Category:Silk_icons'>Wikimedia Commons</a>.</p>
</div>

</div>

</body>
</html>