views:

687

answers:

5

Hi,

I have the following situation
Multiple cameras send images at random intervals via FTP to a predetermined folders.
EG:
recordings/camera1/images/ - for first camera
recordings/camera2/images/ - for second camera
etc
Each of them save images in .jpg format and then close FTP connection.

What I need to get done is to call a PHP script after each time a new file is added. So let us say whenever a new image is added into the /recordings folder I need to call the php script with newimage.php?location=recordings/camera1/images/picture002.jpg etc

The server is a Linux box, FTP - ProFTPD

Any suggestions on how I can get this done?

Thanks.

+1  A: 

I would make a cronjob that looks for new files every 5 or 10 minutes. Then call the PHP script with wget for any new images found.

Update: It seems you need to hook into ProFTPD somehow. Maybe mod_exec can be of help.

Martin Geisler
Please note: Cron job is not an option as there will thousands of files and we are not using a DB. So we cannot find out if there are any new files
ToughPal
Okay, I've added another suggestion after you wrote that crontab was out.
Martin Geisler
+1  A: 

You should be able to use inotify to do this on Linux. There is a PECL module which allows PHP to receive inotify events. The documentation is here.

Travis Beale
+3  A: 

I would advise you to take a look at the mod_exec of ProFTPD. Here is a quote explaining its goal:

The mod_exec module can be used to execute external programs or scripts at various points in the process of handling FTP commands. By conscious design, ProFTPD does not and will not execute external programs. This is a security decision, as it was decided not to allow ProFTPD to serve as a means of compromising a system or disclosing information via bugs in external programs or scripts. Use of this module allows for such external programs to be executed, and also opens up the server to the mentioned possibilities of compromise or disclosure via those programs.

You can also take a look at inotify which offers you to monitor system file activity.

Boris Guéry
+1  A: 

There is an another way.

mod_exec opens your FTP server to security issues. Reading the log files can sometimes cause your server to not write to log file, etc.

Try enabling mod_mysql. Then enable logging to database table, this way you can scan your MySQL table for changes. Try searching for logging using MySQL and proFTPd http://www.iezzi.ch/archives/110

Pasta
+1  A: 

You should use fam_monitor_directory( ) function in PHP (if it was compiled using --with-fam). It does exactly what you need, i.e. execute a PHP program whenever the directory's content changes.

Use something like this in your cronjob (it doesn't matter how many files there are in the directory, the loop goes one time for each change in the directory:

[code]

/* opens a connection to the FAM service daemon */ $fam_res = fam_open ();

/* The second argument is the full pathname of the directory to monitor. */
$nres = fam_monitor_directory ( $fam_res, '/home/www/example.com/cameras/');

while( fam_pending ( $fam_res ) ) {
$arr = (fam_next_event($fam_res)) ;
if ($arr['code']) == FAMCreated ) {
/* deal here with the new file, which name now is stored in $arr['filename'] */
}
}

fam_close($fam_res);

thanks, i could not find any examples of how to use. Also how to specify which php file to call?
ToughPal