Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether a file is still being transferred via ftp

Tags:

php

ftp

I have a directory with files that need processing in a batch with PHP. The files are copied on the server via FTP. Some of the files are very big and take a long time to copy. How can I determine in PHP if a file is still being transferred (so I can skip the processing on that file and process it in the next run of the batch process)?

A possibility is to get the file size, wait a few moments, and verify if the file size is different. This is not waterproof because there is a slight chance that the transfer was simply stalled for a few moments...

like image 746
murze Avatar asked Aug 30 '11 10:08

murze


People also ask

How can I tell if a file is completed on a FTP server?

A possible solution would be first uploading the file with a different filename (e.g. adding ". partial") and then renaming it to its final name. If the server finds the final name then the upload has been completed.

How are files transferred by FTP explain?

File transfer protocol is a way to download, upload, and transfer files from one location to another on the internet and between computer systems. FTP enables the transfer of files back and forth between computers or through the cloud. Users require an internet connection in order to execute FTP transfers.

What will happen when the given FTP command is executed?

If you execute the ftp command and specify the host name (HostName) of a remote host, the ftp command tries to establish a connection to the specified host. If the ftp command connects successfully, the ftp command searches for a local $HOME/. netrc file in your current directory or home directory.


2 Answers

One of the safest ways of doing this is to upload the files with a temporary name, and rename them once the transfer is finished. You program should skip files with the temporary name (a simple extension works just fine.) Obviously this requires the client (uploader) to cooperate, so it's not ideal.

[This also allows you to delete failed (partial) transfers after a given time period if you need that.]

Anything based on polling the file size is racy and unsafe.

Another scheme (that also requires cooperation from the uploader) can involve uploading the file's hash and size first, then the actual file. That allows you to know both when the transfer is done, and if it is consistent. (There are lots of variants around this idea.)

Something that doesn't require cooperation from the client is checking whether the file is open by another process or not. (How you do that is OS dependent - I don't know of a PHP builtin that does this. lsof and/or fuser can be used on a variety of Unix-type platforms, Windows has APIs for this.) If another process has the file open, chances are it's not complete yet.

Note that this last approach might not be fool-proof if you allow restarting/resuming uploads, or if your FTP server software doesn't keep the file open for the entire duration of the transfer, so YMMV.

like image 199
Mat Avatar answered Sep 23 '22 11:09

Mat


Our server admin suggested ftpwho, which outputs which files are currently transferred.

http://www.castaglia.org/proftpd/doc/ftpwho.html

So the solution is to parse the output of ftpwho to see if a file in the directory is being transferred.

like image 26
murze Avatar answered Sep 21 '22 11:09

murze