您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在PHP中使用opendir()按字母顺序排序和显示目录列表

在PHP中使用opendir()按字母顺序排序和显示目录列表

您需要先将文件读入数组,然后才能对它们进行排序。这个怎么样?

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

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

        $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><a href=\"Images/$file\" class=\"thick@R_290_2419@\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";
}

?>

编辑:这与您要的内容无关,但是您也可以使用pathinfo()函数文件扩展名进行更通用的处理。然后,您不需要扩展的硬编码数组,就可以删除任何扩展。

php 2022/1/1 18:19:02 有454人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶