Monitor Filesystem Changes in PHP
System tools & monitoring programs are often required to monitor a directory or file(s) for changes, not just content changes, it may even be attribute changes like permissions, timestamps etc. Inotify is being used for quite some time in C programming to achieve the same.
Here we will be looking at enabling inotify support for PHP, and look at a few code snippets at how about using it. The on criteria for for using inotify is that it'll only work on a Linux system.
Inotify is available as a PECL package, first get PECL from http://pecl.php.net/ if you do not already have it. After doing that just issue the following command as a root user:
Nothing else needs to be done to make it work.
Inotify can monitor for a variety of events like file open, close, new file, changes, etc. you can get the full list here http://php.net/manual/en/inotify.constants.php
Now let's look at a simple program to monitor a directory for newly created files, it's pretty simple, go through the code below:
In many cases we might want to monitor more than one events, like in the above example we might want to monitor for file modification as well, so here's how to do that:
Similarly you can also watch files for changes, I hope this article was helpful. Enjoy.
Re: Monitor Filesystem Changes in PHP
Thank you sir thats the cool and the descriptive way to make me understand.
|All times are GMT +5.5. The time now is 16:43.|