PHP:如何避免读取通过 FTP 推送给我的部分文件?

2023-10-30php开发问题
0

本文介绍了PHP:如何避免读取通过 FTP 推送给我的部分文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

文件正在通过 FTP 推送到我的服务器.我在 Drupal 模块中使用 PHP 代码处理它们.操作系统是Ubuntu,FTP服务器是vsftp.

Files are being pushed to my server via FTP. I process them with PHP code in a Drupal module. O/S is Ubuntu and the FTP server is vsftp.

我会定期检查新文件,使用 SimpleXML 处理它们并将它们移动到完成"文件夹.如何避免处理部分上传的文件?

At regular intervals I will check for new files, process them with SimpleXML and move them to a "Done" folder. How do I avoid processing a partially uploaded file?

vsftp 的 lock_upload_files 默认为 yes.我想先尝试移动文件,期望移动在当前上传的文件上失败.这似乎没有发生,至少在命令行上是这样.如果我开始上传一个大文件并移动,它只会在新位置不断增长.我猜目录项没有被锁定.

vsftp has lock_upload_files defaulted to yes. I thought of attempting to move the files first, expecting the move to fail on a currently uploading file. That doesn't seem to happen, at least on the command line. If I start uploading a large file and move, it just keeps growing in the new location. I guess the directory entry is not locked.

我应该在尝试加载到 SimpleXML 之前尝试使用模式a"或r+"来查看它是否成功,还是有更好的方法来做到这一点?我想我可以检测到 SimpleXML 加载失败,但是……这看起来很乱.

Should I try fopen with mode 'a' or 'r+' just to see if it succeeds before attempting to load into SimpleXML or is there a better way to do this? I guess I could just detect SimpleXML load failing but... that seems messy.

我无法控制发件人.他们不会进行上传和重命名.

I don't have control of the sender. They won't do an upload and rename.

谢谢

推荐答案

如果锁定不起作用,我不知道有什么解决方案像您想要的那样干净/简单.您可以通过不处理上次修改时间的文件来做出有根据的猜测(您可以使用 filemtime()) 在过去 x 分钟内.

If locking doesn't work, I don't know of a solution as clean/simple as you'd like. You could make an educated guess by not processing files whose last modified time (which you can get with filemtime()) is within the past x minutes.

如果您想要比这更高的置信度,您可以检查并存储每个文件的大小(使用 filesize()) 在一个简单的数据库中,并且每 x 分钟检查新大小与旧大小.如果大小在 x 分钟内没有改变,您可以假设没有更多内容被发送.

If you want a higher degree of confidence than that, you could check and store each file's size (using filesize()) in a simple database, and every x minutes check new size against its old size. If the size hasn't changed in x minutes, you can assume nothing more is being sent.

这篇关于PHP:如何避免读取通过 FTP 推送给我的部分文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17