如何使用PHP检查目录内容是否发生变化? [英] How to check if directory contents has changed with PHP?

查看:150
本文介绍了如何使用PHP检查目录内容是否发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP中编写一个照片库脚本,并且有一个用户将存储照片的目录。我正在尝试设置页面缓存,只有当目录的内容发生变化时,才能进行缓存刷新。我以为我可以使用filemtime()函数缓存目录的最后修改时间,并将其与目录的当前修改时间进行比较。但是,正如我已经意识到的那样,目录修改时间不会随着文件被添加或从该目录中删除而改变(至少在Windows上,还不确定Linux机器)。



所以我的问题是,检查目录的内容是否被修改的最简单的方法是什么?

解决方案

p>呃。我只是存储一个目录列表的md5。如果内容更改,md5(目录列表)将更改。您可能得到非常偶然的md5冲突,但我认为这个机会足够小。

或者,您可以在该目录中存储一个小文件,其中包含最后修改日期。但是我会和md5一起去。






PS。在第二个想法中,看到如何查看性能(缓存)请求和哈希目录列表可能不是完全最佳的。


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?

解决方案

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检查目录内容是否发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆