douzhulan1815 2011-03-16 20:47
浏览 62
已采纳

我如何修改readdir()列出文件的方式。

I have a script that creates a zip package daily in a directory. After this is done another script deletes the 'last' file in the directory so I always have just the latest x days.

while (false !== ($file = readdir($handle))) 
    {
            if ($file != "." && $file != "..")
            {
                echo "$file";
                echo '<br>';

                //count files
                $file_count = $file_count + 1;
            }
        }

However suddenly readdir() is returning the list of files like this:

March_16_2011.zip

March_12_2011.zip

March_13_2011.zip

March_14_2011.zip

March_15_2011.zip

So of course rather than the oldest file being removed the newest one is.

When I look at the files in FTP they are all dated correctly.

Why is readdir() returning them out of order in this case? How do I force it to order them in a way I want? (By date)

  • 写回答

7条回答 默认 最新

  • dqkmn26444 2011-03-16 20:56
    关注

    readdir returns the filenames in an arbitrary order, depending on how the OS returns the entries from the filesystem. You need to manually sort the result list:

    foreach (glob("*") as $fn) {
        $files[$fn] = filemtime($fn);
    }
    arsort($files);
    $files = array_keys($files);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题