PHP script that sends email file changes that occurred in a directory / subdirectories

I have a directory with several subdirectories that users add files via FTP. I am trying to develop a php script (which I will run as a cron job) that will check the directory and its subdirectories for any changes in files, file sizes or date changes. I searched long and hard and so far you can only find one script that works, which I tried to change - the original one located here - however, it seems to only send the first notification email, which shows what is indicated in the directories. It also creates a text file for the directory and the contents of the subdirectory, but when the script runs a second time, it seems to crash and I receive an email without the contents.

Does anyone know an easy way to do this in php? The script I found is rather complicated, and I tried for many hours to debug it without success.

Thanks in advance!

+3
source share
2 answers

I love sfFinder so much that I wrote my own adaptation:

http://www.symfony-project.org/cookbook/1_0/en/finder

https://github.com/homer6/altumo/blob/master/source/php/Utils/Finder.php

Ease of use, works well.

However, for your use, depending on the size of the files, I would put everything in a git repository. Then easy to track.

NTN

0
source

Here you go:

$log = '/path/to/your/log.js';
$path = '/path/to/your/dir/with/files/';
$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)
{
    // send email with mail(), SwiftMailer, PHPMailer, ...
    $email = 'The following files have changed:' . "\n" . implode("\n", array_keys($diff));

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

, , . , , $log $path, , , .

, , , , , , , . , :

$result[$file] = sprintf('%u|%u', filesize($file), filemtime($file));

:

$result[$file] = sprintf('%u|%u|%s', filesize($file), filemtime($file), md5_file($file));
// or
$result[$file] = sprintf('%u|%u|%s', filesize($file), filemtime($file), sha1_file($file));

, , - CSV 1-5 .

0

All Articles