为什么这个 PHP 文件夹列表脚本会重复两次



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

.. 等等,但如您所见,顶部目录被扫描了两次。

你应该跳过 . 和 ..

也许是这样的?

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检查对于检查这种东西来说不是那么安全......它假设您没有真正的短命名文件。

相关内容

  • 没有找到相关文章

最新更新