There is a folder in which xml files are beeing copied at no particular time, when an event is happening. I want a php way to inspect the folder's status and when an xml file arrives, an event will be triggered.(ex.call to the xml parser). So which is the best way (in php) to monitor a folder and trigger events according to it's status? Thanx!
-
What does *arrive* mean? – hakre Nov 15 '11 at 11:57
-
2Which operating system are you using? – hakre Nov 15 '11 at 12:20
-
arrives means gets uploaded to the folder (via ftp). I am using both linux & windows. – Stathis Nov 17 '11 at 09:22
-
1Getting uploaded (e.g. the momemnt the first, then again the second, then again the third until then then then and then the final byte has been written), like if a file with 100 bytes get's uploaded you get 100 events? You should really think first here what you're doing exactly. - **Related:** [FTP incoming monitor (on upload)](http://stackoverflow.com/questions/3292552/ftp-incoming-monitor-on-upload); [How to determine wheter a file is still being transferred via ftp](http://stackoverflow.com/questions/7241978/how-to-determine-wheter-a-file-is-still-being-transferred-via-ftp) – hakre Nov 17 '11 at 13:12
-
Related: [Run PHP script when a new file is added via FTP](http://stackoverflow.com/questions/930421/run-php-script-when-a-new-file-is-added-via-ftp) - As you can see with questions similar to yours, this depends on which FTP client you're using. So you should take more details into consideration to find an answer to your question. Use the search to find more related questions to get a better overview as well. – hakre Nov 17 '11 at 13:19
-
possible duplicate of [Fastest way to compare directory state, or hashing for fun and profit](http://stackoverflow.com/questions/4343868/fastest-way-to-compare-directory-state-or-hashing-for-fun-and-profit) – Gordon Mar 06 '13 at 17:50
4 Answers
Haven't tried it, but maybe Inotify can help you:
inotify is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications.
There's a PHP extension for inotify, see InotifyDocs and inotifyPECL.
Another alternative if you're running on linux is to use a PHP-independent daemon to monitor a directory for changes. You can use dnotify for it (obsoleted by inotify), something like:
dnotify -a -r -b -s /path/ -e <command>;
It will execute the command each time one of the files in other folder are modified (-a -r -b -s = any access/recursive directory lookup/run in background/no output).
Related:
You should take a look at FAM (File Alteration Monitor). PHP 4 based binary extension (beta status); documentation.
I think the most simple way to do this is to use cron job to examine the folder every minute. The other option is to trigger your php script from another script/program that copies new xml file to the directory. Cron enables you to run your script every minute. If you want instant response you should write a shell script(http://aplawrence.com/Unixart/watchdir.html) that constanlty runs in the background or maybe pearl daemon to you detect new file and trigger your php script to examine changes.

- 3,280
- 21
- 17