问题描述
文件正在通过 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 推送给我的部分文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!