1

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?

5
  • Have you considered using checksums? Commented Feb 8, 2012 at 11:52
  • I didn't think of that, but it does seem obvious now. You mean calculate the hash, send that somehow along with the file (as a separate file?) and then keep checking it until it is the same? Or is there an easier standard way of doing this? Commented Feb 8, 2012 at 11:56
  • Alasadair, I don't think using checksum is an efficient way to solve this problem. If the file is huge, it would consume time to compute the checksum again and again on the growing file. Commented Feb 8, 2012 at 11:59
  • @SusamPal, yes OK, that makes sense also. Commented Feb 8, 2012 at 12:04
  • have a similar problem, where we have a text file with some data and filenames for accompanied images, how to know if the accompanying files have completed uploading so we can process them Commented Feb 12, 2015 at 8:13

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Some 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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.