简体   繁体   English

根据php中的文件夹状态进行文件夹监控和事件触发

[英]Folder monitoring and event triggering according to folder status in php

There is a folder in which xml files are beeing copied at no particular time, when an event is happening. 当事件发生时,有一个文件夹在其中没有特定时间复制xml文件。 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). 我想用php方式检查文件夹的状态,当xml文件到达时,会触发一个事件。(ex.call到xml解析器)。 So which is the best way (in php) to monitor a folder and trigger events according to it's status? 那么这是监视文件夹并根据它的状态触发事件的最佳方式(在php中)? Thanx! 感谢名单!

Haven't tried it, but maybe Inotify can help you: 没试过,但也许Inotify可以帮助你:

inotify is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. inotify是一个Linux内核子系统,用于扩展文件系统以注意对文件系统的更改,并将这些更改报告给应用程序。

There's a PHP extension for inotify, see Inotify Docs and inotify PECL . 有一个用于inotify的PHP扩展,请参阅Inotify 文档inotify PECL

Another alternative if you're running on linux is to use a PHP-independent daemon to monitor a directory for changes. 如果你在linux上运行,另一种选择是使用独立于PHP的守护进程来监视目录的变化。 You can use dnotify for it (obsoleted by inotify), something like: 您可以使用dnotify (由inotify废弃),例如:

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). 每次修改其他文件夹中的一个文件时,它都会执行命令(-a -r -b -s =任何访问/递归目录查找/在后台/无输出中运行)。

Related: 有关:

You should take a look at FAM (File Alteration Monitor) . 你应该看看FAM(文件更改监视器) PHP 4 based binary extension (beta status); 基于PHP 4的二进制扩展(beta状态); documentation . 文件

I think the most simple way to do this is to use cron job to examine the folder every minute. 我认为最简单的方法是使用cron job每分钟检查一次文件夹。 The other option is to trigger your php script from another script/program that copies new xml file to the directory. 另一个选项是从另一个脚本/程序触发您的php脚本,该脚本/程序将新的xml文件复制到该目录。 Cron enables you to run your script every minute. Cron使您可以每分钟运行脚本。 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. 如果您想要即时响应,您应该编写一个shell脚本(http://aplawrence.com/Unixart/watchdir.html),constanlty在后台运行,或者可能是珍珠守护程序,您可以检测新文件并触发您的php脚本来检查更改。

使用cron定期scandir()目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM