douji6461 2009-07-08 01:07
浏览 43
已采纳

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

I'm not sure how simple this would be, but I'm using a script which displays the files from a specific folder, however I'd like them to be displayed in alphabetical order, would it be hard to do this? Here's the code I'm using:

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>'."
";
                        $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>'."
";
                    }

                }
            }
        }
        closedir($handle); 
    }   
    echo $thelist;

:)

  • 写回答

5条回答 默认 最新

  • doudi7782 2009-07-08 01:12
    关注

    Instead of using readdir you could simply use scandir (documentation) which sorts alphabetically by default.

    The return value of scandir is an array instead of a string, so your code would have to be adjusted slightly, to iterate over the array instead of checking for the final null return value. Also, scandir takes a string with the directory path instead of a file handle as input, the new version would look something like this:

    foreach(scandir($mainframe->getCfg( 'absolute_path' ) ."/images/store/") as $file) {
      // rest of the loop could remain unchanged
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊