opendir不适用于名称间隔的文件或文件夹

时间:2014-12-13 02:49:51

标签: php opendir

我有一大堆我正在使用的代码并且工作正常,唯一的问题是当文件夹或文件名中有空格时,它只链接到(或显示)文件中的第一个单词名称

请帮忙,

感谢

<?php
$dirFiles = array();
// opens images folder
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {

        // strips files extensions      
        $crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-", "error_log", ".php");    

        $newstring = str_replace($crap, " ", $file );   

        //asort($file, SORT_NUMERIC); - doesnt work :(

        // hides folders, writes out ul of images and thumbnails from two folders

        if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
            $dirFiles[] = $file;
        }
    }
    closedir($handle);
}

sort($dirFiles);
foreach($dirFiles as $file)
{
    echo "<li>";
    echo "<a href=".$file.">".$file."<br></li>";
}

?>

1 个答案:

答案 0 :(得分:2)

您有报价问题。您在href属性周围缺少引号:

echo "<a href=".$file.">".$file."<br></li>";

应该是

echo '<a href="'.$file.'">'.$file.'<br></li>';