dp20011 2014-12-07 14:01
浏览 28
已采纳

PHP:如何使用DirectoryIterator处理_alphabetically_文件?

Using this way, after the completion of DirectoryIterator cycle
you will get a list of filenames, and later you will be able to alphabetically sort it,
so that you can process each file with a filename from this list - in alphabetical order...

Now, what if, after the completion of DirectoryIterator cycle,
you need an array (alphabetically sorted by filename), that contains not only the filename,
but also all the other file properties, such as:

  • permissions
  • owner
  • time of creation
  • size
  • etc...

The problem here is, you cant sort before the completion of DirectoryIterator cycle,
and after it you will not be able to access anything other than filenames from your list...

  • 写回答

1条回答 默认 最新

  • douzuan5365 2014-12-07 14:01
    关注

    DirectoryIterator objects provide a simple way to access many of file properties.

    $dir = new DirectoryIterator($path);
    foreach ($dir as $fileInfo) {
        if ((!$fileInfo->isDot())&&($fileInfo->GetExtension() == "txt")) {
            /* You can access the file information inside this cycle */
            $octal_perms = substr(sprintf('%o', $fileInfo->getPerms()), -4);
            echo $fileInfo->getFilename() . " " . $octal_perms . "
    ";
        }
    }
    

    If we need the fileInfo objects after the completion of DirectoryIterator cycle,
    we will have to clone (copy) all these DirectoryIterator objects into a new array,
    and then sort this array alphabetically by filename attribute of DirectoryIterator objects.

    function cmp($a, $b)
    {
        return strcmp($a->getFilename(), $b->getFilename());
    }
    
    $dir = new DirectoryIterator($path);
    foreach ($dir as $fileInfo) {
        if ((!$fileInfo->isDot())&&($fileInfo->GetExtension() == "txt")) {
            /* we need to clone a fileInfo object into array, not just assign it */
            $allFilesInfo[] = clone $fileInfo;
        }
    }
    
    /* Alphabetically sorting the array with DirectoryIterator objects, by filename */
    usort($allFilesInfo, 'cmp');
    
    foreach ($allFilesInfo as $fileInfo) {
        /* Everything is alphabetical here ;) */
        $octal_perms = substr(sprintf('%o', $fileInfo->getPerms()), -4);
        echo $fileInfo->getFilename() . " " . $octal_perms . "
    ";
    }
    

    ^^ In this last cycle, you can work with your files in alphabetical order,
    while being able to access all their properties :)

    NOTE: in case of crash, caused by "too many open files" error,
    increase the max limit of open file descriptors per a process in your OS.
    Related config files are depend on your OS, and usually they're stored in /etc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键