File Listing Scripts
Posted: Tue Feb 07, 2012 12:38 am
This is a file listing script I devised to list audio files
main.css
index.php
main.css
h1 { padding: 0; margin: 0; background: #99FF99; font-family: arial, serif; font-size: 100%; color: #660000; font-weight: 700; text-align: center; width:100%; border: #000000 thin solid; } body { background: #000066; color: #FFFFFF; font-family: arial, serif; font-size: 100%; font-weight: 700; } #scan { border: #FFFFFF thin solid; background: #663366; width: 35%; margin-left:40%; margin-top:15%; } a { color: #00FF00; text-decoration: none; } a:active { color: #00FF00; text-decoration: none; } a:visited { color: #00FF00; text-decoration: none; } a:hover { color: #00FF00; text-decoration: underline; } th { text-align: left; } #tright { text-align: right; }
index.php
<!DOCTYPE html> <html lang="EN"> <head> <title>Directory Listing</title> <link rel="stylesheet" type="text/css" href=" main.css"> <meta charset="utf-8"> </head> <body> <h1>Directory Listing</h1> <div id="scan"> <?php $scan = scandir("stuff"); unset($scan[0]); unset($scan[1]); /* Scan the "stuff" folder and store the resulting array in the $scan variable. Unset elements 0 and 1 of the array, as these are . and .. */ if (empty($scan)) { echo "No files found"; } else { echo "<table style='width: 100%; text-align: left:'> <thead> <tr> <th>Module Code</th> <th>File Count</th> </tr> </thead> <tbody>"; foreach ($scan as $folder) /* This loops through each element of the $scan array and puts each element into a variable known as $folder, and creates a row from it */ { $path = "stuff/". $folder; /* This path variable is kind of hanging over from a previous incarnation of this script and gets used on another page but I couldn't be bothered to take it out */ echo "<tr>\n"; echo "<td><a href='audio.php?name=", $folder, "'>", $folder, "</a></td>\n"; /* This opens the audio page and selects the folder we want */ echo "<td>" , count(scandir($path)) -2, "</td>\n"; /* Because the array contains a . and .. element, we must subtract 2 in order to get the file count */ echo "</tr>\n"; } echo "</tbody>\n"; echo "</table>\n"; } ?> </div> </body> </html>audio.php
<?php $directory = strtoupper($_GET["name"]); /* Processes everything in the name string as uppercase */ $dirscan = scandir("stuff"); /* Scan the stuff folder */ if (!$directory) /* Kill the script if the user doesn't enter a folder name, or enters a folder name that doesn't exist */ { die ("You haven't selected a directory"); } elseif(!in_array($directory, $dirscan)) { die ($directory. " can't be found"); } else { echo "<!DOCTYPE html> <html lang='EN'> <head> <title>Directory Listing for ", $directory, "</title> <link rel='stylesheet' type='text/css' href=' main.css'> <meta charset='utf-8'> </head> <body> <h1>Directory Listing for ", $directory, "</h1> <div id='tright'> <a href='/'>Back</a </div> <div id='scan'>"; $scan = scandir("stuff/". $directory); /* Scans the chosen directory within the stuff folder */ unset($scan[0]); /* Unset the first element */ unset($scan[1]); /* Unset the second element */ if (empty($scan)) { echo "No files found"; } else { echo "<table style='width: 100%; text-align: left:'> <thead> <tr> <th>File Name</th> <th>File Size</th> <th>Date Modified</th> </tr> </thead> <tbody>"; foreach ($scan as $folder) { $trame = substr($folder, 0, -4); /* This is the name of the file before the extension */ $path = "stuff/". $directory. "/". $folder; /* Physical path to the file */ $size = filesize($path); echo "<tr>\n"; echo "<td><a href='stuff/", $folder, "'>", $trame, "</a></td>\n"; /* Link to where the file can be downloaded */ echo "<td>" , number_format($size / 1024000,2), "MB</td>\n"; /* This tales the file size, and displays it in MB */ echo "<td>" , date ("d/m/Y, H:i:s", filemtime($path)), "</td>\n"; /* Date the file was last modified */ echo "</tr>\n"; } echo "</tbody>\n"; echo "</table>\n"; } } ?> </div> </body> </html>Feel free to contact me if you've got questions