'How to detect that a file is being uploaded over FTP

My application is keeping watch on a set of folders where users can upload files. When a file upload is finished I have to apply a treatment, but I don't know how to detect that a file has not finish to upload.

Any way to detect if a file is not released yet by the FTP server?



Solution 1:[1]

I use ftputil to implement this work-around:

  1. connect to ftp server
  2. list all files of the directory
  3. call stat() on each file
  4. wait N seconds
  5. For each file: call stat() again. If result is different, then skip this file, since it was modified during the last seconds.
  6. If stat() result is not different, then download the file.

This whole ftp-fetching is old and obsolete technology. I hope that the customer will use a modern http API the next time :-)

Solution 2:[2]

If you are reading files of particular extensions, then use WINSCP for File Transfer. It will create a temporary file with extension .filepart and it will turn to the actual file extension once it fully transfer the file.

I hope, it will help someone.

Solution 3:[3]

This is a classic problem with FTP transfers. The only mostly reliable method I've found is to send a file, then send a second short "marker" file just to tell the recipient the transfer of the first is complete. You can use a file naming convention and just check for existence of the second file.

You might get fancy and make the content of the second file a checksum of the first file. Then you could verify the first file. (You don't have the problem with the second file because you just wait until file size = checksum size).

And of course this only works if you can get the sender to send a second file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Rama Krishna
Solution 3 A. L. Flanagan