【问题标题】:Sort photos in Php gallery by date按日期对 PHP 库中的照片进行排序
【发布时间】:2023-03-03 06:26:21
【问题描述】:

我正在尝试使用 PHP 为朋友建立一个画廊。目前我的脚本从“图库”文件夹中导入所有图像,并使用自动生成的缩略图和fancybox插件按字母顺序显示它们。 是否可以按日期对它们进行排序?无论是拍摄日期还是上次修改日期都没有关系。我使用的代码如下。提前致谢!

<?php
$path =  'gallery/'; 
$files = scandir('gallery/'); 
?>

<ul>
<?php foreach ($files as $file){
if ($file == '.' || $file == '..'){ 
    echo '';
} else {
?>

<li><a class="fancybox" rel="group" href="<?php echo $path . $file; ?>"><img src="scripts/timthumb.php?src=<?php echo $path . $file; ?>&h=194&w=224&zc=1&q=100" /></a></li>
<?php } }?>
</ul>

【问题讨论】:

    标签: php jquery sorting date gallery


    【解决方案1】:

    此 php 函数按文件的最后修改日期对文件进行排序。 不要忘记将要忽略的文件放入被忽略的文件数组中。

    function scan_dir($dir) {
        $ignored_files = array()
        $files = array();   
        foreach (scandir($dir) as $file) {
            if (in_array($file,$ignored_files) {
                 $files[$file] = filemtime($dir.'/'.$file);
            }
        }
    
        arsort($files);
        $files = array_keys($files);
    
        if(is_null($files))
            return false;
        return $files;
    }
    

    你可以稍微重构一下,这真的很快。希望这会奏效

    【讨论】:

    • 感谢您的回复!不幸的是,该功能似乎不起作用,照片仍然按名称排序,而不是按日期。
    • 您检查过文件[$file] 是否等于上次修改日期?如果你有,你可以对它们进行排序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多