php script to monitor ftp uploads

Discussion in 'Programming/Scripts' started by seahawkja, Nov 7, 2013.

  1. seahawkja

    seahawkja New Member

    Trying to test this script "monitor_ftp.php" at command line.
    (will add as a cron job later).
    Script currently resides in the /public_html directory
    Logged in to server via SSH and cd to public_html

    [~/public_html]# monitor_ftp.php
    -bash: monitor_ftp.php: command not found !! Script IS there!!

    [~/public_html]# php monitor_ftp.php
    Content-type: text/html


    $log = '/path/to/logs/log.js'; !! changed to generic for security!!
    $path = '/path/to/public_ftp/rkeep/';
    $to = "[email protected]";
    $subject = "FTP Monitor Update";
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
    $result = array();

    foreach ($files as $file) {
    if (is_file($file = strval($file)) === true) {
    $result[$file] = sprintf('%u|%u', filesize($file), filemtime($file));

    if (is_file($log) !== true) {
    file_put_contents($log, json_encode($result), LOCK_EX);

    // are there any differences?
    if (count($diff = array_diff($result, json_decode(file_get_contents($log), true))) > 0) {
    $body = 'The following files have changed:' . "\n" . implode("\n", array_keys($diff));

    // send email with mail()
    if (mail($to, $subject, $body)) {
    echo("<p>Email successfully sent!</p>");
    else {
    echo("<p>Email delivery failed…</p>");

    // update the log file with the new file info
    file_put_contents($log, json_encode($result), LOCK_EX);

    Any idea what is going wrong here? All comments greatly appreciated.


  2. Croydon

    Croydon ISPConfig Developer ISPConfig Developer

    You cannot run a php script like that, unless it resides in /usr/bin etc.
    You always have to use
    php monitor_ftp.php
    unless you put a
    #!/usr/bin/php -q
    at the first line of the php file (before the <?php) and make the file executable (chmod u+x monitor_ftp.php)

    Seems the script is not made for running als cli/cron but as a web script. Content-Type is a header sent to the browser to give information about what content type to expect.
  3. seahawkja

    seahawkja New Member

    Checked through and tested via web - email received reporting changes - OK!

    Setup as a cron job to run every 15 minutes. Uploaded some files at various intervals. cron ran the job at desired intervals and script worked reporting changes via email.

    Found out that the host does not allow access to /access-logs directory, so emails were not generated until I changed the location of the log file.

    Thanks for the reply Croydon!

Share This Page