PHP:我如何避免阅读通过FTP推送给我的部分文件? [英] PHP: How do I avoid reading partial files that are pushed to me with FTP?

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

问题描述

文件正通过FTP被推送到我的服务器。我在Drupal模块中用PHP代码处理它们。 O / S是Ubuntu,FTP服务器是vsftp。



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



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



我应该尝试使用模式'a'还是'r +'来试图加载到SimpleXML之前是否成功,或者有一个更好的方法吗?我想我可以检测到SimpleXML加载失败,但似乎很麻烦。



我无法控制发件人。他们不会做一个上传和重命名。



谢谢

>如果锁定不起作用,我不知道解决方案如你所愿是干净/简单的。您可以通过不处理上次修改时间的文件(您可以通过 filemtime() )在过去的 x 分钟内。



如果您想要的信心程度高于您可以检查并存储每个文件的大小(使用 filesize() )在一个简单的数据库中,并且每个 x 分钟都会根据旧的大小检查新大小。如果尺寸在 x 分钟内没有变化,您可以假定没有更多内容正在发送。


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.

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 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.

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.

Thanks

解决方案

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.

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推送给我的部分文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆