我有一个从目录检索文件的脚本,需要按日期对文件进行排序。上传到列表顶部的最新照片。请给任何人提建议?
<?
$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."&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
?>发布于 2014-03-10 15:26:00
您可以使用终端命令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。
发布于 2014-03-10 15:32:14
你应该搜索一下这个。
很明显,opendir()本身并不检索文件修改,也不检索创建日期。
一个简单的搜索"file modified last php“会将你的地址重定向到这里:
https://www.google.com/search?q=php+file+modified+last
其中第一个结果是php.net的doc:
http://php.net/filemtime
你可以在哪里使用
date ("F d Y H:i:s.", filemtime($file)以便检索列表的datetime时间戳。
您可以在数组的顶部构建数组,并通过其中一个数组排序函数(http://www.php.net/manual/en/array.sorting.php)对其进行排序
最后但并非最不重要的一点是,英语是官方编程语言,所以去掉像$celkem和$slozka这样的变量命名
https://stackoverflow.com/questions/22294137
复制相似问题