Is there a way to check if a file is fully uploaded using PHP?

I have a directory on a remote machine where my clients download (via various tools and protocols, from WebDav to FTP). I also have a PHP script that returns a directory structure. Now the problem is that if a client uploads a large file and I make a request during upload, the PHP script will return the file even if it is not fully uploaded. Is there a way to check if a file is fully uploaded using PHP?


source to share

3 answers

Configure the remote server to move uploaded files to a different directory and only directory file requests are moved for files.

AFAIK, there is no way (at least cross-machine) to find out if a file is loaded without doing something like:

  • Request file length
  • Please wait a few seconds.
  • Request file length
  • If it is the same, possibly completed


Most UNIX / Linux / BSD operating systems have a command called lsof

(lsof stands for "list of open files") that lists all currently open files on the system. You can run this command to verify that any process is still running on the file. If not, the download is complete. This example uses awk for filtering, so only files opened by write or read / write file handlers will be shown:

if (shell_exec("lsof | awk '\$4 ~ /.*[uw]/' | grep " . $uploaded_file_name) == '') {
    /* No file handles open for this file, so upload is finished. */


I'm not very familiar with Windows servers, but this thread can help you do the same on a Windows machine (if you have one): How can I tell if a file is open on Windows?



I think some operating systems include a ".part" file when the file is loaded, so there might be a way to check for such a file. Otherwise, I agree with Brian's answer. If you have used the script on the same system, it is enough to simply say using move_uploaded_file () return if it was uploaded with PHP script but it becomes a call from a remote directory that can be added with different protocols.



All Articles