繁体   English   中英

PHP opendir(),按日期排序

[英]PHP opendir(), sorting by date

我有一个脚本,可以从目录中检索文件,并且需要按日期对文件进行排序。 最新的照片上传到列表的顶部。 请告知任何人?

<?
$slozka = "./gallery/holiday/"; //select the folder from which you want to list files
$thumb= "thumb"; //the name of the folder thumbnails
$vypis = opendir($slozka); //open folder
$celkem = '0'; //beginning number of photos

while (false!==($file = readdir($vypis))) //reading files
{ 
    if($file!="."&&$file!=".."&&!is_dir($file)&&$file!=$thumb) //search through folder
    { 
        $celkem++; //count the number of pictures
        $filetitle = $file;
        $nahrada = array("_", ".jpg", ".png", ".gif");
        $filetitle = str_replace($nahrada, " ", "$filetitle");

        if (file_exists($slozka.$thumb.'/'.$file))
        { //If there is a preview and display it ..
            echo "<li><a href=\"gallery/holiday/".$file."\" alt=\"".$file."\"
title=\"".$filetitle."\" class=\"holiday\" /><img src=\"gallery/holiday/thumb/".$file."\" alt=\"".$file."\"></a><span class=\"nazvy\">".$filetitle."</span></li>";
        }//If there is no way it will create ...
        else 
            echo "<li><a href=\"gallery/holiday/".$file."\" alt=\"".$file."\" class=\"holiday\" /><img src=\"thumb.php?nazev=".$file."&amp;cesta=".$slozka."\" alt=\"".$file."\"></a><span class=\"nazvy\">".$filetitle."</span></li>";
    } 
}     
echo "</ul><div id=\"soucet\">Celkem fotek : ".$celkem."</div>"; // print the number of photos in the gallery ...
closedir($vypis); //close folder
?>

您可以使用终端命令find功能从目录中获取最新文件。 进行反向排序,并使用要检查的文件模式循环目录。 然后,您将按日期获取最新文件。

检查链接以供参考。

https://superuser.com/questions/294161/unix-linux-find-and-sort-by-date-modified

Ex :

find . -type f -name '{$fileFormat}*'.txt -exec ls -tr {} \; | sort -r | head -10
$output = array();
@chdir($destinationDir);
@exec($command, $output);

然后循环$output

您应该已经搜索过。

在本机上很明显, opendir()不会检索文件修改,也不会创建日期。

简单搜索“文件修改后的最后一个php”,即可将您重定向到此处:

https://www.google.com/search?q=php+file+modified+last

第一个结果是php.net的文档:

http://php.net/filemtime

在哪里可以使用

date ("F d Y H:i:s.", filemtime($file)

为了检索您的列表的日期时间时间戳。

您可以在其顶部构建数组,并使用数组排序功能之一( http://www.php.net/manual/en/array.sorting.php )对其进行排序

最后但并非最不重要的一点-英语是官方编程语言,因此请摆脱诸如$celkem$slozka类的变量命名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM