【问题标题】:Get most recent file added to folder php [duplicate]获取添加到文件夹 php 的最新文件 [重复]
【发布时间】:2017-10-10 14:01:44
【问题描述】:

我正在尝试显示添加到投资组合项目文件夹中以用作特色图像的最新图像 - 下面的代码在从指定文件夹获取图像方面做得很好,但是它没有显示最后一张上传...有什么建议吗?

<?php
$files= glob('./uploads/test/'. $user->id .'/'.$item->id.'/*');
$count = 0;
$user_avatar = '/images/noimage.jpg'; 
if (!file_exists('./uploads/test/'. $user->id .'/'.$item->id.'/*')) { 
  $user_avatar = '/images/noimage.jpg';
} 

 // otherwise continue
foreach ($files as $file) {
$count++;
if ($count > 0 && is_file($file)) { 
$user_avatar = $file;
} 
}
?> 

【问题讨论】:

  • 查看filemtime() - php.net/manual/en/function.filemtime.php的文档
  • 除非您有一个名为* 的实际文件(我对此表示怀疑),否则此file_exists('./uploads/test/'. $user-&gt;id .'/'.$item-&gt;id.'/*')总是 返回false。该函数调用中不能有通配符。

标签: php


【解决方案1】:

请使用 filemtime 比较文件时间戳。您可以这样做以获取最新消息:

$files = glob("*.*.html");
usort($files, function($a,$b){
  return filemtime($a) - filemtime($b);
});

【讨论】:

  • 请标记以关闭在社区其他地方得到适当回答的问题。 @Magnus 提名了我刚刚敲击此页面的页面(当我关闭页面时,他的链接被自动删除)。这意味着,如果您关注他的链接,您会发现您的建议已经存在。发布答案时,系统更难删除多余的页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 2016-07-22
  • 2017-04-08
  • 1970-01-01
  • 2013-09-03
相关资源
最近更新 更多