代码:
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>
..等等,但正如您所看到的,顶级目录将被扫描两次。
答案 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
检查对于检查此类事情是不安全的......它假设您没有真正的短命名文件。)