Advertisement
Wilds

Display Styled Directory Contents

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