PHP(文件夹)文件列表按字母顺序排列?

Sou*_*aby 5 php directory file alphabetized

我不确定这会有多简单,但是我使用的是一个显示特定文件夹中文件的脚本,但是我希望它们按字母顺序显示,这样做会很难吗?这是我正在使用的代码:

if ($handle = opendir($mainframe->getCfg( 'absolute_path' ) ."/images/store/")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..")  {
                if (($file != "index.html")&&($file != "index.php")&&($file != "Thumbs.db")) {
                $strExt = end(explode(".", $file));
                    if ($strExt == 'jpg') {
                        $Link = 'index.php?option=com_shop&task=deleteFile&file[]='.$file;
                        $thelist .= '<tr class="row0"><td nowrap="nowrap"><a href="'.$Link.'">'.$file.'</a></td>'."\n";
                        $thelist .= '<td align="center" class="order"><a href="'.$Link.'" title="delete"><img src="/administrator/images/publish_x.png" width="16" height="16" alt="delete"></a></td></tr>'."\n";
                    }

                }
            }
        }
        closedir($handle); 
    }   
    echo $thelist;
Run Code Online (Sandbox Code Playgroud)

:)

Chr*_*cks 14

而不是使用readdir你可以简单地使用scandir(文档)默认按字母顺序排序.

返回值scandir是一个数组而不是一个字符串,因此您的代码必须稍微调整,以迭代数组而不是检查最终的null返回值.另外,scandir使用目录路径而不是文件句柄作为输入的字符串,新版本看起来像这样:

foreach(scandir($mainframe->getCfg( 'absolute_path' ) ."/images/store/") as $file) {
  // rest of the loop could remain unchanged
}
Run Code Online (Sandbox Code Playgroud)