PHP脚本,用于发送电子邮件列表文件更改已在目录/子目录中发生的更改

时间:2022-04-26 08:03:25

I have a directory with a number of subdirectories that users add files to via FTP. I'm 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 the files, file sizes or dates modified. I've searched long and hard and have so far only found one script that works, which I've tried to modify - original located here - however it only seems to send the first email notification showing me what is listed in the directories. It also creates a text file of the directory and subdirectory contents, but when the script runs a second time it seems to fall over, and I get an email with no contents.

我有一个目录,其中包含许多用户通过FTP添加文件的子目录。我正在尝试开发一个php脚本(我将作为一个cron作业运行),它将检查目录及其子目录中的文件,文件大小或修改日期的任何更改。我搜索得很长很难,到目前为止只找到一个有效的脚本,我试图修改 - 原来位于这里 - 但它似乎只发送第一封电子邮件通知显示目录中列出的内容。它还会创建目录和子目录内容的文本文件,但是当脚本第二次运行时,它似乎会崩溃,我收到一封没有内容的电子邮件。

Anyone out there know a simple way of doing this in php? The script I found is pretty complex and I've tried for hours to debug it with no success.

那里的任何人都知道在PHP中这样做的简单方法吗?我发现的脚本非常复杂,我已经尝试了几个小时来调试它但没有成功。

Thanks in advance!

提前致谢!

2 个解决方案

#1


0  

I like sfFinder so much that I wrote my own adaption:

我非常喜欢sfFinder,所以我写了自己的改编:

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

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

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

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

Simple to use, works well.

使用简单,效果很好。

However, for your use, depending on the size of the files, I'd put everything in a git repository. It's easy to track then.

但是,为了您的使用,根据文件的大小,我将所有内容放在git存储库中。这很容易跟踪。

HTH

HTH

#2


0  

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);
}

I am assuming you know how to send an e-mail. Also, please keep in mind that the $log file should be kept outside the $path you want to monitor, for obvious reasons of course.

我假设您知道如何发送电子邮件。此外,请记住,$ log文件应保留在您要监视的$ path之外,当然,原因很明显。

After reading your question a second time, I noticed that you mentioned you want to check if the files change, I'm only doing this check with the size and date of modification, if you really want to check if the file contents are different I suggest you use a hash of the file, so this:

在第二次阅读你的问题之后,我注意到你提到你要检查文件是否发生变化,我只是检查修改的大小和日期,如果你真的想检查文件内容是否不同我建议你使用文件的哈希值,所以这个:

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

Becomes this:

变成这样:

$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));

But bare in mind that this will be much more expensive since the hash functions have to open and read all the contents of your 1-5 MB CSV files.

但请记住,由于哈希函数必须打开并读取1-5 MB CSV文件的所有内容,因此这将更加昂贵。

#1


0  

I like sfFinder so much that I wrote my own adaption:

我非常喜欢sfFinder,所以我写了自己的改编:

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

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

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

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

Simple to use, works well.

使用简单,效果很好。

However, for your use, depending on the size of the files, I'd put everything in a git repository. It's easy to track then.

但是,为了您的使用,根据文件的大小,我将所有内容放在git存储库中。这很容易跟踪。

HTH

HTH

#2


0  

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);
}

I am assuming you know how to send an e-mail. Also, please keep in mind that the $log file should be kept outside the $path you want to monitor, for obvious reasons of course.

我假设您知道如何发送电子邮件。此外,请记住,$ log文件应保留在您要监视的$ path之外,当然,原因很明显。

After reading your question a second time, I noticed that you mentioned you want to check if the files change, I'm only doing this check with the size and date of modification, if you really want to check if the file contents are different I suggest you use a hash of the file, so this:

在第二次阅读你的问题之后,我注意到你提到你要检查文件是否发生变化,我只是检查修改的大小和日期,如果你真的想检查文件内容是否不同我建议你使用文件的哈希值,所以这个:

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

Becomes this:

变成这样:

$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));

But bare in mind that this will be much more expensive since the hash functions have to open and read all the contents of your 1-5 MB CSV files.

但请记住,由于哈希函数必须打开并读取1-5 MB CSV文件的所有内容,因此这将更加昂贵。