Run PHP script when a new file is added via FTP

2020-06-22 16:33发布

问题:

I have multiple cameras sending images at random intervals via FTP to a predetermined folders.

For example:
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.

I need to call a PHP script after each time a new file is added. Let's 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

and so on.

The server is a Linux box running ProFTPD

How I can get this done?

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.

回答1:

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.



回答2:

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:

/* 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);


回答3:

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.



回答4:

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.



回答5:

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



回答6:

if you are under linux just execute this code

inotifywait -m /path/to/folder -e create -e moved_to |
while read path action file; do
    echo "The file '$file' appeared in directory '$path' via '$action'"
    # do something with the file for example
    php index.php upload '$path/$file'
done


标签: php linux ftp