首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP opendir(),按日期排序

PHP opendir(),按日期排序
EN

Stack Overflow用户
提问于 2014-03-10 15:15:13
回答 2查看 2.4K关注 0票数 0

我有一个从目录检索文件的脚本,需要按日期对文件进行排序。上传到列表顶部的最新照片。请给任何人提建议?

代码语言:javascript
复制
<?
$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
?>
EN

回答 2

Stack Overflow用户

发布于 2014-03-10 15:26:00

您可以使用终端命令find函数从目录中获取最新文件。使用您正在检查的文件模式进行反向排序和循环目录。然后,您将获得该日期的最新文件。

请检查链接以供参考。

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

代码语言:javascript
复制
Ex :

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

然后循环$output

票数 0
EN

Stack Overflow用户

发布于 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

你可以在哪里使用

代码语言:javascript
复制
date ("F d Y H:i:s.", filemtime($file)

以便检索列表的datetime时间戳。

您可以在数组的顶部构建数组,并通过其中一个数组排序函数(http://www.php.net/manual/en/array.sorting.php)对其进行排序

最后但并非最不重要的一点是,英语是官方编程语言,所以去掉像$celkem$slozka这样的变量命名

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22294137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档