PHP 脚本发送电子邮件列出目录/子目录中发生的文件更改

发布于 2024-11-05 12:00:31 字数 496 浏览 1 评论 0原文

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

有人知道在 php 中执行此操作的简单方法吗?我发现的脚本非常复杂,我尝试了几个小时来调试它但没有成功。

提前致谢!

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.

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.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-11-12 12:00:31

给你:

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

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

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.

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.

人心善变 2024-11-12 12:00:31

我非常喜欢 sfFinder,因此我编写了自己的改编版本:

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

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

使用简单,效果良好。

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

华泰

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

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

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.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文