为什么这个php文件夹列表脚本重复自己两次?

时间:2013-10-31 19:20:05

标签: php file directory scandir directory-traversal

嘿,我现在只是建立一个小的PHP脚本列出目录中的文件夹和文件(不担心安全性或验证 - 我只使用)但由于某种原因 - 它似乎扫描基目录两次,然后转移到下一个。我已经尝试了各种方法来修复它,但似乎没有任何工作,你能帮助我吗?

代码:

function getall() {
  $base = scandir('files/');
  echo "<select>";
  foreach ($base as $x) { // main loop start
    if (strlen($x) > 3) {
  $x = str_replace(".","",$x);
      echo "<option class='folder' value='/" . $x . "/'>" . $x ."</option>\n";  // display item as option select
                        }
      $folders = scandir('files/' . $x . '/'); // scan next folder
      foreach ($folders as $y) { // get files in folder
        if (strlen($y) > 3) {
          $y = str_replace("..","",$y);
          echo "<option class='file' value='./". $x ."/" . $y . "'>" . $y .                 "</option>\n"; // display item as option select
        }
      }
    }
    echo "</select>";
  }

输出是:

<select>
  <option class='file' value='./Class'>Class</option>
  <option class='file' value='./Conditionals'>Conditionals</option>
  <option class='file' value='./DateTime'>DateTime</option>
  <option class='file' value='./File'>File</option>
  <option class='file' value='./Folder'>Folder</option>
  <option class='file' value='./Functions'>Functions</option>
  <option class='file' value='./Loops'>Loops</option>
  <option class='file' value='./MySQL'>MySQL</option>
  <option class='file' value='./Security'>Security</option>
  <option class='file' value='./String'>String</option>
  <option class='file' value='../files'>files</option>
  <option class='file' value='../files.php'>files.php</option>
  <option class='file' value='../index.php'>index.php</option>
  <option class='folder' value='/Class/'>Class</option>
  <option class='file' value='Class/createclass.txt'>createclass.txt</option>
  <option class='folder' value='/Conditionals/'>Conditionals</option>
  <option class='file' value='Conditionals/if.txt'>if.txt</option>
  <option class='file' value='Conditionals/ifelse.txt'>ifelse.txt</option>
  <option class='file' value='Conditionals/ifelseif.txt'>ifelseif.txt</option>
  <option class='file' value='Conditionals/switch.txt'>switch.txt</option>
  <option class='folder' value='/DateTime/'>DateTime</option>
  <option class='file' value='DateTime/date.txt'>date.txt</option>
  <option class='file' value='DateTime/time.txt'>time.txt</option>
  <option class='folder' value='/File/'>File</option>

..等等,但正如您所看到的,顶级目录将被扫描两次。

1 个答案:

答案 0 :(得分:1)

你应该跳过。和..

也许是这样的?

function getall() {
  $base = scandir('files/');
  echo "<select>";
  foreach ($base as $x) { // main loop start
    if (strlen($x) > 3) {
      $x = str_replace(".","",$x);
      echo "<option class='folder' value='/" . $x . "/'>" . $x ."</option>\n";  // display item as option select
         // I moved from here the }
      $folders = scandir('files/' . $x . '/'); // scan next folder
      foreach ($folders as $y) { // get files in folder
        if (strlen($y) > 3) {
          $y = str_replace("..","",$y);
          echo "<option class='file' value='./". $x ."/" . $y . "'>" . $y .                 "</option>\n"; // display item as option select
          }
        }
      }
    } // to here
    echo "</select>";
  }

(否则strlen检查对于检查此类事情是不安全的......它假设您没有真正的短命名文件。)