How to check if directory contents has changed with PHP?(如何检查目录内容是否已使用 PHP 更改?)
问题描述
我正在用 PHP 编写照片库脚本,并且有一个用户将在其中存储图片的目录.我正在尝试设置页面缓存并仅在目录内容发生更改时才刷新缓存.我想我可以通过使用 filemtime() 函数缓存目录的最后修改时间并将其与目录的当前修改时间进行比较来做到这一点.但是,正如我逐渐意识到的那样,目录修改时间不会随着文件的添加或从该目录中删除而改变(至少在 Windows 上,对 Linux 机器还不确定).
I'm writing a photo gallery script in PHP and have a single directory where the user will store their pictures. I'm attempting to set up page caching and have the cache refresh only if the contents of the directory has changed. I thought I could do this by caching the last modified time of the directory using the filemtime() function and compare it to the current modified time of the directory. However, as I've come to realize, the directory modified time does not change as files are added or removed from that directory (at least on Windows, not sure about Linux machines yet).
所以我的问题是,检查目录内容是否已被修改的最简单方法是什么?
So my questions is, what is the simplest way to check if the contents of a directory have been modified?
推荐答案
呃.我只是存储目录列表的 md5.如果内容改变,md5(directory-listing) 也会改变.您可能遇到非常偶然的 md5 冲突,但我认为这种机会已经很小了..
或者,您可以在该目录中存储一个包含上次修改"日期的小文件.但我会选择 md5.
Uh. I'd simply store the md5 of a directory listing. If the contents change, the md5(directory-listing) will change. You might get the very occasional md5 clash, but I think that chance is tiny enough..
Alternatively, you could store a little file in that directory that contains the "last modified" date. But I'd go with md5.
附注.再想一想,看看您如何看待请求和散列目录列表的性能(缓存)可能不是完全最佳的..
PS. on second thought, seeing as how you're looking at performance (caching) requesting and hashing the directory listing might not be entirely optimal..
这篇关于如何检查目录内容是否已使用 PHP 更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查目录内容是否已使用 PHP 更改?
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
