PHP 处理 FTP 放置目录中的文件
我想在服务器上创建一个 FTP drop,可以通过 FTP 将文件上传到某些目录,然后由 PHP 脚本进行处理(作为 cron 作业或守护进程运行)。
通过 FTP 传输文件或从目录中读取文件都没有问题。但是 PHP 脚本如何知道文件上传是否已完成或仍在进行中?
我可以反复检查文件大小在一段时间内是否发生变化,如果1分钟内没有变化则处理文件。但这似乎不是一个优雅的解决方案,我想它有时可能会出错,例如上传因某种原因暂停或失败。
另一种方法是在每个文件后上传一个空白文本文件,并使用名称的变体(例如 competed.filename
)。然后脚本可以检查这些文件是否存在并处理相应的文件。但这也不是一个优雅的解决方案,并且可能容易在某个地方出错。
有没有好的方法可以用 PHP 检查 FTP 文件上传是否成功完成?
I want to create an FTP drop on a server, where files can be uploaded to certain directories via FTP, and then be processed by a PHP script (run as a cron job or daemon).
It's no problem to FTP the files, or to read them from the directory. But how would the PHP script know if the file upload is complete or still in progress?
I could repeatedly check if the filesize has changed over a period of time, and process the files if it does not change for 1 minute. But this does not seem like an elegant solution and I imagine it could go wrong at times, such as if the upload is paused or fails for some reason.
Another way could be to upload a blank text file after each file, with a variation on the name (such as competed.filename
). Then the script could check for the presence of these files and process the corresponding files. But this is also not an elegant solution and possibly prone to going wrong somewhere.
Is there a good way to check with PHP if an FTP file upload has completed successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个脚本来监视接收 FTP 服务器的日志文件。将 tail 固定到日志文件的末尾并等待新文件达到“完成”状态。并非所有服务器都提供此类日志条目。如果做不到这一点,可以使用内核挂钩来确定文件何时被 WRITE_CLOSED,就像 FTP 服务器完成上传时一样。
这两种方法我都在 Perl 和 Python 中成功完成,但我对 PHP 不太确定。由于这将是在命令行上运行的脚本,因此如果您可以跟踪 FTP 日志以获取完整条目,我建议您尝试使用 Perl,如果您需要添加内核挂钩,则建议您尝试使用 Python。
Python:http://pyinotify.sourceforge.net/
Perl:http://metacpan.org/pod/File::Tail
还有另一种替代方法不适合这些方法中的任何一种,这可能是使用 FTP服务器,例如 drftpd,它允许利用其“zipscript”插件按照建议进行日志记录,根据 .sfv 文件执行文件校验和。然而,这需要大量配置,并且可能不适合您的设置。
祝你好运。
You can create a script which watches the receiving FTP server's log file. Fix tail onto the end of the log file and wait for new files to achieve 'complete' status. Not all servers provide such a log entry. Failing that, it is possible to use kernel hooks to determine when a file has been WRITE_CLOSED, as it would when the FTP server is done with an upload.
Both methods I have completed succesfully in Perl and Python, but I'm not so sure about PHP. Since this would be a script run on the command line, I suggest you try this using Perl if you can tail the FTP log for complete entries, or Python if you need to add kernel hooks.
Python: http://pyinotify.sourceforge.net/
Perl: http://metacpan.org/pod/File::Tail
There is another alternative failing either of these methods being suitable, that might be to use an FTP server such as drftpd, which allows the logging as suggested by utilising it's "zipscript" plugin, performing checksums of files against an .sfv file. However this requires a lot of configuration and might not be suitable depending on your setup.
Good luck.
某些 FTP 服务器支持文件在上传时具有特殊后缀的机制。上传完成后,它们将被重命名为其最终目标文件名。
ProFTPD 例如将此称为
HiddenStores
:http://www.proftpd.org/docs/directives/linked/config_ref_HiddenStores.html" rel="nofollow">http://www.proftpd.org proftpd.org/docs/directives/linked/config_ref_HiddenStores.htmlSome FTP servers support a mechanism where files have a special suffix while they are being uploaded. Upon completion of the upload they are renamed to their final destination filename.
ProFTPD e.g. calls this
HiddenStores
: http://www.proftpd.org/docs/directives/linked/config_ref_HiddenStores.html如果您想要一个纯 PHP 解决方案,按照您概述的方式检查文件是否发生更改是我过去使用的解决方案。
如果您想要立即处理,而不是等待 1 分钟,您将需要修改实际的 FTP 服务器。某些 FTP 服务器允许您将其配置为在上传时执行操作 - 该操作可能是调用您的 PHP 脚本以及文件名。
用 PHP 创建 FTP 服务也是可能的,但可能需要更多的工作……但是几乎可以肯定,学习和配置已经编程、测试和体验过的 FTP 服务器会更容易。
If you want a pure PHP solution, checking the files for changes as you outlined is the solution I have used in the past.
If you want immediate processing, and not waiting 1 minute, you will need to modify the actual FTP server. Some FTP servers will allow you to configure them to take an action upon upload - that action could be to call your PHP script along with the filename.
It is also possible, but probably much more work, to create an FTP service in PHP... but it is almost certainly easier to learn and configure an FTP server that is already programmed, tested, and experienced.