Home  >  Article  >  Backend Development  >  Is there any way to use php to monitor changes in files in a folder?

Is there any way to use php to monitor changes in files in a folder?

WBOY
WBOYOriginal
2016-08-04 09:20:122161browse

1. I saw on stack overflow that someone said to use md5 to implement the link http://stackoverflow.com/ques.... Please tell me how to achieve it, 3Q
2. Are there any other methods to achieve monitoring?

Reply content:

1. I saw on stack overflow that someone said to use md5 to implement the link http://stackoverflow.com/ques.... Please tell me how to achieve it, 3Q
2. Are there any other methods to achieve monitoring?

1. Get all the files under this directory through scandir();
2. foreach traverse the files and use md5file() to save the md5 value of the file to the file OR database of other directories (why not arrays and other variables? Because the variables will be cleared every time it is executed);
3. Use cron or windows scheduled tasks to execute, convert the previous md5 list into an array, and then use array_diff() to compare the previous md5 list with the current scandir() The obtained md5 file list, if there is a result returned, it means that some files have been modified, deleted or added.

You can try mtime and ctime of the directory

The Linux kernel has an event-driven Inotify mechanism (C document) that supports monitoring directory and file changes.
PHP also has a PECL extension that binds this set of Inotify operations.
http://pecl.php.net/package/ i...
http://cn.php.net/manual/zh/b...
Or you can use PHP to call the command tool provided by inotify-tools.

Based on what the two brothers said above, it depends on your needs. If it is real-time, use inofiy. If you need to determine file differences similar to when submitting code, you can use file fingerprints such as md5 values. .

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn