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:

There's no generic solution to this problem.

Some FTP servers lock the file being uploaded, preventing you from accessing it, while the file is still being uploaded. For example IIS FTP server does that. Most other FTP servers do not. See my answer at Prevent file from being accessed as it's being uploaded.


There are some common workarounds to the problem (originally posted in SFTP file lock mechanism, but relevant for the FTP too):

  • You can have the client upload a "done" file once the upload finishes. Make your automated system wait for the "done" file to appear.

  • You can have a dedicated "upload" folder and have the client (atomically) move the uploaded file to a "done" folder. Make your automated system look to the "done" folder only.

  • Have a file naming convention for files being uploaded (".filepart") and have the client (atomically) rename the file after upload to its final name. Make your automated system ignore the ".filepart" files.

    See (my) article Locking files while uploading / Upload to temporary file name for an example of implementing this approach.

    Also, some FTP servers have this functionality built-in. For example ProFTPD with its HiddenStores directive.

  • A gross hack is to periodically check for file attributes (size and time) and consider the upload finished, if the attributes have not changed for some time interval.

  • You can also make use of the fact that some file formats have clear end-of-the-file marker (like XML or ZIP). So you know, that the file is incomplete.


Some FTP servers allow you to configure a hook to be called, when an upload is finished. You can make use of that. For example ProFTPD has a mod_exec module (see the ExecOnCommand directive).

Solution 2:

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 3:

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.