Advertisement
Wilds

Display Styled Directory Contents

Nov 10th, 2013
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // Adds pretty filesizes
  4. function pretty_filesize($file) {
  5.     $size = filesize($file);
  6.     if ($size < 1024) {
  7.         $size = $size . " Bytes";
  8.     } elseif (($size < 1048576) && ($size > 1023)) {
  9.         $size = round($size / 1024, 1) . " KB";
  10.     } elseif (($size < 1073741824) && ($size > 1048575)) {
  11.         $size = round($size / 1048576, 1) . " MB";
  12.     } else {
  13.         $size = round($size / 1073741824, 1) . " GB";
  14.     }
  15.     return $size;
  16. }
  17.  
  18. date_default_timezone_set("Europe/Rome");
  19.  
  20. // Checks to see if veiwing hidden files is enabled
  21. /*
  22.   if ($_SERVER['QUERY_STRING'] == "hidden") {
  23.     $hide = "";
  24.     $ahref = "./";
  25.     $atext = "Hide";
  26.   } else {
  27.  
  28.     $hide = ".";
  29.     $ahref = "./?hidden";
  30.     $atext = "Show";
  31.   }
  32.  */
  33.  
  34. $hide = ".";
  35.  
  36. if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || substr($_SERVER['QUERY_STRING'],0,2) == ".." || strstr($_SERVER['QUERY_STRING'], "..")) {
  37.     $currdir = ".";
  38. } else {
  39.     $currdir = urldecode($_SERVER['QUERY_STRING']);
  40. }
  41.  
  42. if ($currdir == ".")
  43.     $label = "Root";
  44. else {
  45.     $path = explode('/', $currdir);
  46.     $label = $path[count($path)-1];
  47. }
  48.  
  49. ?>
  50.  
  51. <!doctype html>
  52. <html>
  53.     <head>
  54.         <meta charset="UTF-8">
  55.         <link rel="shortcut icon" href="./.favicon.ico">
  56.         <title><?= $label ?></title>
  57.  
  58.         <link rel="stylesheet" href="./.style.css">
  59.         <script src="./.sorttable.js"></script>
  60.     </head>
  61.  
  62.     <body>
  63.         <div id="container">
  64.             <h1><?= $label ?></h1>
  65.  
  66.             <table class="sortable">
  67.                 <thead>
  68.                     <tr>
  69.                         <th>Nome file</th>
  70.                         <th>Tipo</th>
  71.                         <th>Dimensione</th>
  72.                         <th>Ultima modifica</th>
  73.                     </tr>
  74.                 </thead>
  75.                 <tbody>
  76. <?php
  77.  
  78. // Opens directory
  79. $myDirectory = opendir($currdir);
  80.  
  81. // Gets each entry
  82. while ($entryName = readdir($myDirectory)) {
  83.     $dirArray[] = $entryName;
  84. }
  85.  
  86. // Closes directory
  87. closedir($myDirectory);
  88.  
  89. // Counts elements in array
  90. $indexCount = count($dirArray);
  91.  
  92. // Sorts files
  93. //sort($dirArray);
  94.  
  95. // Loops through the array of files
  96. for ($index = 0; $index < $indexCount; $index++) {
  97.  
  98.     // Decides if hidden files should be displayed, based on query above.
  99.     if (substr("$dirArray[$index]", 0, 1) != $hide || ($currdir != '.' && $dirArray[$index] == "..")) {
  100.  
  101.         // Resets Variables
  102.         $favicon = "";
  103.         $class = "file";
  104.  
  105.         // Gets File Names
  106.         $name = $dirArray[$index];
  107.         $namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index];
  108.     $fullname = $currdir . '/' . $dirArray[$index];
  109.  
  110.         // Gets Date Modified
  111.         $modtime = date("M j Y g:i A", filemtime($fullname));
  112.         $timekey = date("YmdHis", filemtime($fullname));
  113.  
  114.  
  115.         // Separates directories, and performs operations on those directories
  116.         if (is_dir($currdir . '/' . $dirArray[$index])) {
  117.             $extn = "&lt;Cartella&gt;";
  118.             $size = "&lt;Cartella&gt;";
  119.             $sizekey = "0";
  120.             $class = "dir";
  121.  
  122.             // Gets favicon.ico, and displays it, only if it exists.
  123.             if (file_exists("$namehref/favicon.ico")) {
  124.                 $favicon = " style='background-image:url($namehref/favicon.ico);'";
  125.                 $extn = "&lt;Website&gt;";
  126.             }
  127.  
  128.             // Cleans up . and .. directories
  129.             if ($name == ".") {
  130.                 $name = ". (Current Directory)";
  131.                 $extn = "&lt;System Dir&gt;";
  132.                 $favicon = " style='background-image:url($namehref/.favicon.ico);'";
  133.             }
  134.             if ($name == "..") {
  135.                 $name = ".. (Cartella Precedente)";
  136.                 $extn = "&lt;System Dir&gt;";
  137.             }
  138.             if ($currdir == "." && $dirArray[$index] == "..")
  139.                 $namehref = "";
  140.             elseif ($dirArray[$index] == "..") {
  141.                 $dirs = explode('/', $currdir);
  142.                 unset($dirs[count($dirs) - 1]);
  143.                 $prevdir = implode('/', $dirs);
  144.                 $namehref = '?' . $prevdir;
  145.             }
  146.             else
  147.                 $namehref = '?' . $namehref;
  148.         }
  149.  
  150.         // File-only operations
  151.         else {
  152.             // Gets file extension
  153.             $extn = pathinfo($dirArray[$index], PATHINFO_EXTENSION);
  154.  
  155.             // Prettifies file type
  156.             switch ($extn) {
  157.                 case "png": $extn = "PNG Image";
  158.                     break;
  159.                 case "jpg": $extn = "JPEG Image";
  160.                     break;
  161.                 case "jpeg": $extn = "JPEG Image";
  162.                     break;
  163.                 case "svg": $extn = "SVG Image";
  164.                     break;
  165.                 case "gif": $extn = "GIF Image";
  166.                     break;
  167.                 case "ico": $extn = "Windows Icon";
  168.                     break;
  169.  
  170.                 case "txt": $extn = "Text File";
  171.                     break;
  172.                 case "log": $extn = "Log File";
  173.                     break;
  174.                 case "htm": $extn = "HTML File";
  175.                     break;
  176.                 case "html": $extn = "HTML File";
  177.                     break;
  178.                 case "xhtml": $extn = "HTML File";
  179.                     break;
  180.                 case "shtml": $extn = "HTML File";
  181.                     break;
  182.                 case "php": $extn = "PHP Script";
  183.                     break;
  184.                 case "js": $extn = "Javascript File";
  185.                     break;
  186.                 case "css": $extn = "Stylesheet";
  187.                     break;
  188.  
  189.                 case "pdf": $extn = "PDF Document";
  190.                     break;
  191.                 case "xls": $extn = "Spreadsheet";
  192.                     break;
  193.                 case "xlsx": $extn = "Spreadsheet";
  194.                     break;
  195.                 case "doc": $extn = "Microsoft Word Document";
  196.                     break;
  197.                 case "docx": $extn = "Microsoft Word Document";
  198.                     break;
  199.  
  200.                 case "zip": $extn = "ZIP Archive";
  201.                     break;
  202.                 case "htaccess": $extn = "Apache Config File";
  203.                     break;
  204.                 case "exe": $extn = "Windows Executable";
  205.                     break;
  206.  
  207.                 default: if ($extn != "") {
  208.                         $extn = strtoupper($extn) . " File";
  209.                     } else {
  210.                         $extn = "Sconosciuto";
  211.                     } break;
  212.             }
  213.  
  214.             // Gets and cleans up file size
  215.             $size = pretty_filesize($fullname);
  216.             $sizekey = filesize($fullname);
  217.         }
  218.  
  219.         // Output
  220.         echo("
  221.         <tr class='$class'>
  222.             <td><a href='$namehref'$favicon class='name'>$name</a></td>
  223.             <td><a href='$namehref'>$extn</a></td>
  224.             <td sorttable_customkey='$sizekey'><a href='$namehref'>$size</a></td>
  225.             <td sorttable_customkey='$timekey'><a href='$namehref'>$modtime</a></td>
  226.         </tr>");
  227.     }
  228. }
  229. ?>
  230.  
  231.                 </tbody>
  232.             </table>
  233.  
  234.         <!-- <h2><?php echo("<a href='$ahref'>$atext hidden files</a>"); ?></h2> -->
  235.         </div>
  236.     </body>
  237. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement