如何检查目录内容是否已使用 PHP 更改?

2022-01-01 00:00:00 directory php last-modified filemtime

我正在用 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..

相关文章