我有一个PHP脚本按字母排列文件夹,但由于某种原因它也得到一个空白目录..这是代码:
<?php
$directory = opendir("albums/");
$items = array();
while($items[] = readdir($directory))
sort($items);
closedir($directory);
foreach ($items as $item)
{
if(($item != ".") && ($item != "..")){
$files[] = $item;
$addon = "php";
$class1 = "image";
$class2 = "comment";
$class3 = "name";
$class4 = "starcomment";
$class5 = "arrow";
$imglist='';
$img_folder = "albums/$item/";
mt_srand((double)microtime()*1000);
$imgs = dir($img_folder);
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
$imglist .= "$item ";
} closedir($imgs->handle);
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
$random = mt_rand(0, $no);
$image = $imglist[$random];
$style = "background-image: url('thumbnail.php?file=$img_folder$image&width=147&height=83')";
$dirc = "rtl";
$pics = "תמונות";
$directory2 = opendir("albums/$item");
while($item2 = readdir($directory2)){
if(($item2 != ".") && ($item2 != "..")){
$files2[] = $item2;
$sizeofarray = count($files2) -1;
}}
$myFile = "albums/$item/text/text.txt";
$lines = file($myFile);
echo "<li><a href=$item.$addon>
<span class=\"".$class1."\" style=\"".$style."\"></span><span class=\"".$class2."\" dir=\"".$dirc."\">$lines[0]</span><span class=\"".$class3."\" dir=\"".$dirc."\">$lines[1]</span><span class=\"".$class4."\" dir=\"".$dirc."\"> $sizeofarray $pics</span><span class=\"".$class5."\"></span>
</a></li>";
unset($sizeofarray, $files2, $item2);
}
}
?>
我知道其中一些不相关,但以防它们是...... 当我测试它时,它会显示a.php,b.php .... .php
出了什么问题?
答案 0 :(得分:1)
更改
while($items[] = readdir($directory))
到
while($item = readdir($directory))
{
if (!empty(trim($item)) && $item != '.' && $item != '..')
$items[] = $item;
}
应该修复它。