加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP包括具有更改文件名的图像文件

发布时间:2020-12-13 21:39:48 所属栏目:PHP教程 来源:网络整理
导读:我是 PHP的新手,所以请原谅我,如果这个问题看起来非常苛刻.并提前谢谢你. 我需要在另一个页面上包含从网络摄像头生成的jpg.但是我只需要包含最新的jpg文件.不幸的是,网络摄像头为每个jpg创建一个唯一的文件名.如何使用include或其他功能仅包含最新的图像文件
我是 PHP的新手,所以请原谅我,如果这个问题看起来非常苛刻.并提前谢谢你.
我需要在另一个页面上包含从网络摄像头生成的jpg.但是我只需要包含最新的jpg文件.不幸的是,网络摄像头为每个jpg创建一个唯一的文件名.如何使用include或其他功能仅包含最新的图像文件?
(通常文件名是这样的2011011011231101.jpg,代表year_month_date_timestamp).

解决方法

简单的方法是在下面的代码的帮助下获得最新的图像

$path = "/path/to/my/dir"; 

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  // could do also other checks than just checking whether the entry is a file
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
      $latest_ctime = filectime($filepath);
      $latest_filename = $entry;
    }
  }
}

// now $latest_filename contains the filename of the newest file

将最新图像的来源提供给< img>标签

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读