g3x0

Simple PHP directory navigation

Jan 3rd, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3.     // Verificam extensia
  4.     function _is($file, $ext) {
  5.         if (substr(strrchr(strtolower($file), '.'), 1) == $ext)
  6.             return 1;
  7.         else
  8.             return 0;
  9.     }
  10.  
  11.     // Functia principala
  12.     function navigate_dir($dir) {
  13.  
  14.         // Daca directorul oferit e valid, atunci
  15.         if ($handle = opendir($dir)) {
  16.  
  17.             // Afisam directorul curent
  18.             echo 'Current Wording Directory: ' . getcwd() . '<br />';
  19.  
  20.             // Incepem generarea formularului si setam butonul "Go up"
  21.             // Inputul hidden "prevdir" retine directorul curent si-l trimite prin POST
  22.             // pentru a functiona cum trebuie butonul "Back"
  23.             echo '<form action="" method="post">
  24.                     <input type="hidden" name="prevdir" value="' . getcwd() . '" />
  25.                     <input type="submit" name="gotodir" value="' . $dir . '/.." title="Go up" />';
  26.            
  27.             // Setam butonul "Back" doar dupa ce s-a accesat macar o data un alt director
  28.             if (isset( $_POST['prevdir'])) {
  29.                 echo '
  30.                     <input type="hidden" name="prevdir" value="' . getcwd() . '" />
  31.                     <input type="submit" name="gotodir" value="' . $_POST['prevdir'] . '" title="Back" />';
  32.             }
  33.  
  34.             // Afisam prima data directoarele
  35.             while (false !== ($entry = readdir($handle))) {
  36.                 if ($entry != "." && $entry != "..") {
  37.                     if (is_dir($entry))
  38.                         echo '
  39.                             <input type="hidden" name="prevdir" value="' . getcwd() . '" />
  40.                             <br /><input type="submit" name="gotodir" value="' . $entry . '" />';
  41.                 }
  42.             }
  43.            
  44.             // Inchidem formul pentru ca nu ne mai este de folos
  45.             echo '</form>';
  46.             closedir($handle);
  47.  
  48.             $handle = opendir($dir);
  49.  
  50.             // Afisam fisierele imediat dupa directoare
  51.             while (false !== ($entry = readdir($handle))) {
  52.                 if ($entry != "." && $entry != "..") {
  53.                     if (! is_dir($entry))
  54.                         if (_is ($entry, 'txt') ||
  55.                             _is ($entry, 'png') ||
  56.                             _is ($entry, 'jpg') ||
  57.                             _is ($entry, 'jpeg') ||
  58.                             _is ($entry, 'zip') ||
  59.                             _is ($entry, 'tar') ||
  60.                             _is ($entry, 'gz') ||
  61.                             _is ($entry, 'css'))
  62.                             echo "<a href=\"./$dir/$entry\">$entry</a><br />";
  63.                         else
  64.                             echo "$entry<br />";
  65.                 }
  66.             }
  67.            
  68.             // Inchidem directorul
  69.             closedir($handle);
  70.         }
  71.     }
  72.  
  73.     if (isset($_POST['gotodir'])) {
  74.         // Navigam pe directorul selectat daca s-a accesat unul
  75.         navigate_dir($_POST['gotodir']);
  76.     } else {
  77.         // Daca nu este accesat niciun director inseamna ca s-a accesat
  78.         // pagina pentru prima oara si deschidem directorul principal
  79.         navigate_dir('.');
  80.     }
  81.  
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment