按日期列出目录,它会从数组中删除一个项目..我是不是错过了什么



代码:

    $files = array();
    $dir = new DirectoryIterator($directory);
    foreach ($dir as $fileinfo) {
       $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
    }
    //krsort will sort in reverse order
    krsort($files);
    foreach($files as $file){
        if ($file == "." or $file == ".." or $file == "thumbs" or $file == "Thumbs.db" or strpos($file, '.') == TRUE){
        }else{
            if (file_exists($directory.$file.'/zconfirmed.txt')){
                echo '<script>console.log("display_subdirectories.php says: '.$directory.$file.'/zconfirmed.txt");</script><li class=""><a class="w3-btn w3-green" href="' .$URL.$file. '" >' .$file. '</a></li>';
            }
            else{
                echo '<script>console.log("display_subdirectories.php says: '.$directory.$file.'");</script><li class="bold"><a class="w3-btn w3-red" href="' .$URL.$file. '" >' .$file. '</a></li>';
            }
        }
    }

这是我的问题。如果只有一个目录中有zconfirmed.txt文件,那么当我遍历数组时,它不会出现。如果有2个或更多,它工作得很好。有人有答案,甚至我可以做些什么来更好地解决这个代码吗?

我只是想按上次修改日期/时间的顺序列出目录。

非常感谢你们的帮助!

您已经将文件名存储在数组中了,难道不能将foreach循环更改为类似的更简单的循环吗

foreach($files as $file) {
    if ($file == "zconfirmed.txt") {
        echo '<script>console.log("display_subdirectories.php says: '.$directory.$file.'/zconfirmed.txt");</script><li class=""><a class="w3-btn w3-green" href="' .$URL.$file. '" >' .$file. '</a></li>';
    }
}

这似乎在起作用:

  echo '<div id="subdirectories"><h3>Subdirectories:</h3><ul>';
    $files = scandir($directory); 
    $dir = array();
    foreach ($files as $file){
        if (checkDirectory($file) == true){
            $filetime = filemtime($directory.$file);
            $filename = $file;
            $dir[$filetime] =  $filename;
        }
    }
    ksort($dir);
    $files = $dir;
    foreach ($files as $time=>$file){
        $contiansPhotos = FALSE;
        $path = $directory.$file;
        $dirs = glob($path.'/{zconfirmed.txt}', GLOB_BRACE);
        foreach($dirs as $dir) {
            $contiansPhotos = TRUE;
        }
        if($contiansPhotos === TRUE){
            echo '<li class=""><a class="w3-btn w3-green" href="' .$URL.$file. '" >' .$file. '</a></li>';
        }
    }//foreach $files as $file;
    foreach ($files as $time=>$file){
        $contiansPhotos = FALSE;
        $path = $directory.$file;
        $dirs = glob($path.'/{zconfirmed.txt}', GLOB_BRACE);
        foreach($dirs as $dir) {
            $contiansPhotos = TRUE;
        }
        if($contiansPhotos === FALSE){
            echo '<li class="bold"><a class="w3-btn w3-red" href="' .$URL.$file. '" >' .$file. '</a></li>';
        }
    }//foreach $files as $file;
    echo '</ul></div>';

相关内容

  • 没有找到相关文章

最新更新