Folder monitoring and event triggering according to folder status in php

徘徊边缘 提交于 2020-01-24 08:46:28

问题


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!


回答1:


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:

  • Read file change in php (linux equivalent of tail -f )
  • How to efficiently monitor a directory for changes on linux?



回答2:


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:


You should take a look at FAM (File Alteration Monitor). PHP 4 based binary extension (beta status); documentation.




回答4:


Use cron to regularly scandir() the directory.



来源:https://stackoverflow.com/questions/8135842/folder-monitoring-and-event-triggering-according-to-folder-status-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!