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

php noob在这里-我将这个脚本拼凑在一起,以显示带有opendir的文件夹中的图像列表,但是我无法弄清楚如何(或在哪里)按字母顺序对数组进行排序


<?php


// 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") {

    echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";}

}

closedir($handle);

}


?>

任何建议或指针将不胜感激!


跃然一笑
浏览 788回答 3
3回答

蓝山帝景

您需要先将文件读入数组,然后才能对它们进行排序。这个怎么样?<?php$dirFiles = array();// opens images folderif ($handle = opendir('Images')) {&nbsp; &nbsp; while (false !== ($file = readdir($handle))) {&nbsp; &nbsp; &nbsp; &nbsp; // strips files extensions&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $crap&nbsp; &nbsp;= array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $newstring = str_replace($crap, " ", $file );&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //asort($file, SORT_NUMERIC); - doesnt work :(&nbsp; &nbsp; &nbsp; &nbsp; // hides folders, writes out ul of images and thumbnails from two folders&nbsp; &nbsp; &nbsp; &nbsp; if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dirFiles[] = $file;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; closedir($handle);}sort($dirFiles);foreach($dirFiles as $file){&nbsp; &nbsp; echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"&nbsp; </a></li>\n";}?>编辑:这与您要的内容无关,但是您也可以使用pathinfo()函数对文件扩展名进行更通用的处理。然后,您不需要扩展的硬编码数组,就可以删除任何扩展。

富国沪深

使用 opendir()opendir()不允许对列表进行排序。您必须手动执行排序。为此,首先将所有文件名添加到数组中,然后使用进行排序sort():$path = "/path/to/file";if ($handle = opendir($path)) {&nbsp; &nbsp; $files = array();&nbsp; &nbsp; while ($files[] = readdir($dir));&nbsp; &nbsp; sort($files);&nbsp; &nbsp; closedir($handle);}并随后用一一列举foreach:$blacklist = array('.','..','somedir','somefile.php');foreach ($files as $file) {&nbsp; &nbsp; if (!in_array($file, $blacklist)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "<li>$file</a>\n <ul class=\"sub\">";&nbsp; &nbsp; }}使用 scandir()使用,这要容易得多scandir()。默认情况下,它将为您执行排序。使用以下代码可以实现相同的功能:$path = "/path/to/file";$blacklist = array('somedir','somefile.php');// get everything except hidden files$files = preg_grep('/^([^.])/', scandir($path));&nbsp;foreach ($files as $file) {&nbsp; &nbsp; if (!in_array($file, $blacklist)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "<li>$file</a>\n <ul class=\"sub\">";&nbsp; &nbsp; }}使用DirectoryIterator(首选)$path = "/path/to/file";$blacklist = array('somedir','somefile.php');foreach (new DirectoryIterator($path) as $fileInfo) {&nbsp; &nbsp; if($fileInfo->isDot()) continue;&nbsp; &nbsp; $file =&nbsp; $path.$fileInfo->getFilename();&nbsp; &nbsp; echo "<li>$file</a>\n <ul class=\"sub\">";}

翻翻过去那场雪

这就是我会做的方式if(!($dp = opendir($def_dir))) die ("Cannot open Directory.");while($file = readdir($dp)){&nbsp; &nbsp; if($file != '.')&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $uts=filemtime($file).md5($file);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $fole_array[$uts] .= $file;&nbsp; &nbsp; }}closedir($dp);krsort($fole_array);foreach ($fole_array as $key => $dir_name) {&nbsp; #echo "Key: $key; Value: $dir_name<br />\n";}
打开App,查看更多内容
随时随地看视频慕课网APP