Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting whether a file is complete or partial in PHP

Tags:

php

copy

upload

In this system, SFTP will be used to upload large video files to a specific directory on the server. I am writing a PHP script that will be used to copy completely uploaded files to another directory.

My question is, what is a good way to tell if a file is completely uploaded or not? I considered taking note of the file size, sleeping the script for a few seconds, reading the size again, and comparing the two numbers. But this seems very kludgy.

like image 217
mattalxndr Avatar asked Aug 20 '12 13:08

mattalxndr


2 Answers

Simple

If you just want a simple technique that will handle most use cases - write a process to check the last modified date or (as indicated in the question) the file size and if it hasn't changed in a set time, say 1 minute, assume the upload is finished and process it. Infact - your question is a near duplicate of another question showing exactly this.

Robust

An alternative to polling the size/last modified time of a file (which with a slow or intermittent connection could cause an open upload to be considered complete too early) is to listen for when the file has finished being written with inotify.

Listening for IN_CLOSE_WRITE you can know when an ftp upload has finished updating a specific file.

I've never used it in php, but from the spartan docs it looks that that aught to work in exactly the same way as the underlying lib.

Make FTP clients work for you

Take into account how some ftp programs work, from this comment:

it's worth mentioning that some ftp servers create a hidden temporary file to receive data until the transfer is finished. I.e. .foo-ftpupload-2837948723 in the same upload path as the target file

If applicable, from the finished filename you can therefore tell if the ftp upload has actually finished or was stopped/disconnected mid-upload.

like image 170
AD7six Avatar answered Oct 21 '22 10:10

AD7six


In theory, in case you are allowed to run system commands from PHP you can try to get the pids of your sftp server processes with pidof command and then use lsof output to check if open file descriptors exist for the currently checked upload file.

like image 32
Eugene Avatar answered Oct 21 '22 12:10

Eugene