Stathis
Stathis

Reputation: 111

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. 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!

Upvotes: 3

Views: 6064

Answers (4)

hakre
hakre

Reputation: 197777

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:

Upvotes: 2

breiti
breiti

Reputation: 1185

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

Upvotes: 0

Eugene
Eugene

Reputation: 3375

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.

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151594

Use cron to regularly scandir() the directory.

Upvotes: -1

Related Questions