Advertisement
Wilds

Display Styled Directory Contents

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