hey im just building a little php script at the moment to list folders and files inside a directory (not worried about security or validation - my use only) but for some reason - it seems to scan the base directory twice before moving to the next. Ive tried all kinds of methods to fix it, but nothing seems to be working, can you help me?

Code:

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>";
  }

output is:

<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>

.. and more, but as you can see, the top directory is getting scanned twice.

有帮助吗?

解决方案

You should skip . and ..

Maybe something like this?

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>";
  }

(otherwise the strlen check is not so safe for checking this kind of things... It assumes you don't have real short named files.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top