<?php
$dirs = Array(
  "/var/downloads/",
  "/tmp/files/",
  "./"
  );

if(isSet($_GET["dir"]) && $_GET["dir"] != ""){
  $dir = $_GET["dir"];
  if(in_array($dir, $dirs)){
    if(!$fd = @opendir($dir)){
      echo "Zawartość katalogu nie jest niedostępna!";
      exit;
    }
    $directories = Array();
    $files = Array();
    $others = Array();

    $contents = "";
    while (($file = readdir($fd)) !== false){
      if($file != "." && $file != ".."){
        if(is_dir($dir."/".$file)){
          $directories[] = $file;
        }
        else if(is_file($dir."/".$file)){
          $files[] = $file;
        }
        else{
          $others[] = $file;
        }
      }
    }
    closedir($fd);
?>

<table style="width:100%;">
<tr><td style="background-color:#E0E0E0;text-align:center">
Katalogi:</td></tr>

<?php foreach($directories as $val) echo "<tr><td>$val</td></tr>"; ?>

<tr><td style="background-color:#E0E0E0;text-align:center">
Pliki:</td></tr>

<?php foreach($files as $val) echo "<tr><td>$val</td></tr>"; ?>

<tr><td style="background-color:#E0E0E0;text-align:center">
Inne:</td></tr>

<?php 
    foreach($others as $val) echo "<tr><td>$val</td></tr>";
  }
  else{
    echo "Nieprawidłowy katalog!";
  }
?>

</table>

<?php
}
else{
  echo "Niepoprawne dane.";
}
?>
