0

嘿,我现在只是构建一个小 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>

.. 还有更多,但正如您所见,顶层目录被扫描了两次。

4

1 回答 1

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检查对于检查这类事情并不那么安全......它假设您没有真正的短命名文件。)

于 2013-10-31T19:37:08.940 回答