Does anyone know how to get a progress bar for an upload in php? I am trying writing code for a photo album uploader. I would like a progress bar to display while the photos are uploading.
I am fairly new to php so I dont know everything about it.
Does anyone know how to get a progress bar for an upload in php? I am trying writing code for a photo album uploader. I would like a progress bar to display while the photos are uploading.
I am fairly new to php so I dont know everything about it.
This is by far (after hours of googling and trying scripts) the simplest to set up and nicest uploader I've found
https://github.com/FineUploader/fine-uploader
It doesn't require APC or any other external PHP libraries, I can get file progress feedback on a shared host, and it claims to support html5 drag and drop (personally untested) and multiple file uploads.
One PHP-ish (5.2+) & no-Flash way that worked nicely for me:
First, see this post explaining how to get "uploadprogress" extension up and running.
Then, in the page containing the form that you are uploading file(s) from, create the following iframe:
Next, add this code to your "Submit" button:
Now you have a hidden iframe in your form that will come visible and show contents of uploadprogress.php when you click "Submit" to start uploading files. $upload_id must be the same that you are using as the value of hidden field "UPLOAD_IDENTIFIER" in your form.
The uploadprogress.php itself looks about like this (fix and adjust to your needs):
Note that is self-refreshes every second. You can surely add some nice visual progress bar here (like 2 nested <div>s with different colors) if you like. The iframe with upload progress naturally only works while the upload is in progress, and ends its visible life once the form is submitted and browser reloads to the next page.
I'm sorry to say that to the best of my knowledge a pure PHP upload progress bar, or even a PHP/Javascript upload progress bar is not possible because of how PHP works. Your best bet is to use some form of Flash uploader.
AFAIK This is because your script is not executed until all the superglobals are populated, which includes $_FILES. By the time your PHP script gets called, the file is fully uploaded.
EDIT: This is no longer true. It was in 2010.
Implementation of the upload progress bar is easy and doesn't require any additional PHP extension, JavaScript or Flash. But you need PHP 5.4 and newer.
You have to enable collecting of the upload progress information by setting the directive
session.upload_progress.enabled
toOn
inphp.ini
.Then add a hidden input to the HTML upload form just before any other file inputs. HTML attribute
name
of that hidden input should be the same as the value of the directivesession.upload_progress.name
fromphp.ini
(eventually preceded bysession.upload_progress.prefix
). Thevalue
attribute is up to you, it will be used as part of the session key.HTML form could looks like this:
When you send this form, PHP should create a new key in the
$_SESSION
superglobal structure which will be populated with the upload status information. The key is concatenatedname
andvalue
of the hidden input.In PHP you can take a look at populated upload information:
The output will look similarly to the following:
There is all the information needed to create a progress bar — you have the information if the upload is still in progress, the information how many bytes is going to be transferred in total and how many bytes has been transferred already.
To present the upload progress to the user, write an another PHP script than the uploading one, which will only look at the upload information in the session and return it in the JSON format, for example. This script can be called periodically, for example every second, using AJAX and information presented to the user.
You are even able to cancel the upload by setting the
$_SESSION[$key]['cancel_upload']
totrue
.For detailed information, additional settings and user's comments see PHP manual.
Another uploader full JS : http://developers.sirika.com/mfu/
have fun
HTML5 introduced a file upload api that allows you to monitor the progress of file uploads but for older browsers there's plupload a framework that specifically made to monitor file uploads and give information about them. plus it has plenty of callbacks so it can work across all browsers