Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php how to test if file has been uploaded completely

Tags:

php

Is there any way to check if file has been uploaded completely on the server? My scenario: User uploads file over ftp and my other PHP task is running in cronjob. Now I would like to check if file has been uploaded or if user is still uploading. It is essential because then I know if I can work with that file or wait until it is uploaded. Thank you.

like image 930
peter Avatar asked Apr 01 '12 13:04

peter


People also ask

How can I show uploaded files in PHP?

php define ('MAX_FILE_SIZE', 1000000); $permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg', 'text/plain'); if ($_FILES['file']['type'] == $permitted && $_FILES['file']['size'] > 0 && $_FILES['file']['size'] <= MAX_FILE_SIZE) { move_uploaded_file($_FILES, $uploadedfile); echo ('<img src="'.

How do you check if file is uploaded or not in Javascript?

length property in jQuery to check the file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.

Where does PHP store uploaded files?

php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php. ini. Note that for uploads, those files might be removed as soon as the script the file was uploaded to was terminated (so unless you delay that script, you probably won't see the uploaded file).


2 Answers

If you have control over the application doing the upload, you can require that it upload the file to name.tmp, and when it's done uploading rename it to name.final. Your PHP script could look only for *.final names.

like image 162
Barmar Avatar answered Sep 28 '22 10:09

Barmar


I had the same scenario and found a quick solution that worked for me:

While a file is uploading via FTP, the value of filemtime($yourfile) is continuously modified. When time() minus filemtime($yourfile) is more than X, uploading has stopped. In my scenario, 30 was a good value for x, you might want to use any diferent value, but it should usefully be at least 3.

I do know that this method doesn’t guarantee the file’s integrity, but, as no one but me is going to upload, i dare to assume that.

like image 25
Rusty Avatar answered Sep 28 '22 08:09

Rusty