Blog

Back 45 Comments

A Useful PHP File Upload Progress Meter



UPDATE - Click here for a new (better) PHP Upload Progress Bar Script


If you're like me, you want a PHP upload progress meter that is actually useful in your web applications. All of the upload progress meters that I've run across only include the file upload field by itself, which is not realistic in an actual web application situation. You will most likely be entering other information along with uploading a file. The demo is an adaptation this php file upload progress meter.

The demo below will show you how to display a progress upload meter while also entering other information within the same form.

The progress meter will only show if you actually choose a file to upload. If you are just entering information in the text fields, then the progress meter will not show.


Single File Upload


Multiple File Uploads


YUI Code Library

You need this to display the progress meter bar. Look at demo source code for essential JS files.


Requirements:
You must have APC (Alternative PHP Cache) and PHP 5.2 installed on your server for any of this to work.

This is really the only potential problem for those who don't have root access to their server. It's not that difficult to install, but you either have to have root access, or your hosting provider will need to do this for you. The script will definitely not work without this.

Installing APC on your Linux server
Installing APC on your Windows server
Important: Make sure to include apc.rfc1867 = on in your php.ini file after APC is installed. Your php.ini file should be located in the /etc/ directory.


Resources
Upload Progress Meter by Rasmus Lerdorf
This is where the majority of my script came from. In my opinion, this is the best and cleanest php upload progress meter on the web.

sandeep
December 18, 2008 11:08pm
hi thanks for the code. I have used this code but could not upload a file.I have installed APC and i m using php version 5.2.3, apache version 2.2.4 . I have also included apc.rfc1867 = on in my php.ini file.When i upload a file it does not show any error and shows that file has been uploaded successfully,But does NOT upload file in the destination folder.

PLEASE HELP .IT`S VERY URGENT......
John
December 18, 2008 11:12pm
Check the following:

1. Use phpinfo() to see if APC is really installed.
2. Make sure your folder has full write permissions (777). If you're uploading to the folder where you files reside, then that folder must also be set to 777.

Sandeep
December 19, 2008 12:13am
HI john
Thanks for your quick response.
All things were in place. The problem was related to the caching of my browser.
Now it is working fine.
But now when i am uploading a file , all time during uploading progress bar does not move. But after some time file uploads successfully.

Any idea Please........
John
December 19, 2008 12:39am
Does it work correctly if you use the exact code that I've used?
Sandeep
December 19, 2008 3:55am
Yeah it does, Apart from visualization of "progress" of progress bar
sandeep
December 21, 2008 10:55pm
i am already using apc.rfc1867 = on in my php.ini.
i am using yui 2.6.0. Does that make any difference??
sourabh
December 23, 2008 2:48am
@sandeep

check for "progress.php" file reference in john's code. change this filename according to your filename. i copied his code and saved it as "index.php" obviously, it didn't worked, then i renamed the file to "progress.php" and it worked. i also used the same YUI files as john's. Hope it helps

@John
Gr88888 work man, i m definitely gonna use it in my office projects :)
John
December 23, 2008 9:08am
I believe Sandeep's issue was that he was testing it locally, so the file uploaded instantly, thus not showing any incremental progress on the progress bar. Also, you can rename the progress.php file and do a find/replace to replace 'progress.php' with the name of your file.
phani krishna
January 10, 2009 12:56am
Hi Jhon,

I saw the demo of your script it's looking good. Here i have one doubt. I already having my own uploading script. From there i am able to upload files into my server properly. I want to integrate the progress bar of your script into my script. I cant install your whole scrit for file uploading because i have many options in the backend in my existing script, so i want only the progress bar module of your script. Can i extract that part and use in my script? If yes please guide me.

Thanks
Phani Krishna
John
January 10, 2009 10:30am
Phani - I would need to see your current code to determine how you should integrate the progress bar. Feel free to email it.
Andy
January 15, 2009 3:19am
Hi John, this is a neat script! Thank you.
I have a working php script to handle my uploaded files. Now I would like to use your / Rasmus' script to do the upload. But I don't know where I would refer to my script or do move_uploaded_file(). Could you give me a hint? Thanks, Andy
Andy
January 15, 2009 11:22am
...I got it...
mark
February 9, 2009 7:26am
hi john,
is there a way to get an error-message if the text-field is empty - before starting the upload script? can you help me?
Slavi
February 16, 2009 11:45pm
Is there a way to check the size with APC and then stop the upload if the file is bigger than 10 MB ?
Valentin
March 6, 2009 6:26am
Hi,
i want to use these beautifull script.
when i am uploading a file , all time during uploading progress bar does not move. But after some time file uploads successfully.
i've try this with your code but there is the same problem.

here my apc configuration, is a mistake?
apc
APC Support enabled
Version 3.1.0-dev
MMAP Support Disabled
Locking type File Locks
Revision $Revision: 3.151 $
Build Date Nov 8 2007 23:20:54

Directive Value
apc.cache_by_default On
apc.enable_cli Off
apc.enabled On
apc.file_update_protection 2
apc.filters no value
apc.gc_ttl 3600
apc.include_once_override Off
apc.localcache Off
apc.localcache.size 512
apc.max_file_size 50M
apc.num_files_hint 1000
apc.report_autofilter Off
apc.rfc1867 Off
apc.rfc1867_freq 0
apc.rfc1867_name APC_UPLOAD_PROGRESS
apc.rfc1867_prefix upload_
apc.shm_segments 1
apc.shm_size 64
apc.slam_defense 0
apc.stat On
apc.stat_ctime Off
apc.ttl 0
apc.user_entries_hint 4096
apc.user_ttl 0
apc.write_lock On

any idea?

i'm sorry about my bad english, i'm from germany.

thanks.
David
March 12, 2009 2:04am
Am I misreading the manual where it says:
Note that the file upload tracking is not threadsafe at this point, so new uploads that happen while a previous one is still going will disable the tracking for the previous.

Does this mean that this will cause problems with two concurrent file uploads? eg. progress bar for one person fails if another person starts uploading?
josy
March 24, 2009 5:03am
please help
where is these files?








and progress.php
josh
March 26, 2009 9:55am
Hi John, you're post is fairly recent and I'm hoping you can help me with my APC woes. I have a site where I'm attempting to run your code. The site is multiplesmarket.com/UploadDemo.php. The problem I'm having, and that I have with my own tests, is that apc_fetch just doesn't work.

I've copied the php file, the specified YUI files, and I have APC enabled (multiplesmarket.com/info.php). Can you take a look? By default, my host runs Apache 1.3. I'm wondering if that could be it.
John
March 26, 2009 10:47am
Josh - I'm not positive what could be causing the problem you're running into, but my guess (like yours) is that it's either that you're not running Apache 2 - or your server is running Apache in CGI mode (Server API CGI/FastCGI).
Subeesh
April 3, 2009 7:53am
Hi,
I have tried this out, but the problem is, after pressing the submit button, on a fraction of a second the progres bar seems displayed, but after that, it displays some text like

{"total":9372,"current":9372,"rate":74976,"filename":"exchg_myoffers_main.tpl.html","name":"file","temp_filename":"\/tmp\/php1663g0","cancel_upload":0,"done":1}

What may be the problem? I saw in the code, style.css is included.. Fomr where does I get this style ?

Thanks in advance
Subeesh
Emerson
April 19, 2009 3:23pm
Hi Valentin

Important: Make sure to include apc.rfc1867 = on in your php.ini file after APC is installed.

look bellow

...
apc.report_autofilter Off
apc.rfc1867 Off ----> here
apc.rfc1867_freq 0
...
Manolo
April 16, 2009 7:27am
Hi Johnboy,

I have your demo running quite well in my local server but I would like to integrate it in a complex form that I am developing.

This form has a lot of hidden input fields and several buttond to download files and go back to other form which uses the hidden input field in this one.

Is there any way to run you progress bar iframed to avoid data lossess? You use GET to pass the key, so I loss all the form data!

Thanks in advance

Ingve
May 7, 2009 1:19pm
Loved the script.
Great work, dude..
bill
May 15, 2009 9:35am
where are the uploaded files stored ?
John
May 15, 2009 9:56am
In my example, they are stored in the same directory with the upload script. You can set the location yourself here:

$uploaddir = ""; //set this to where your files should be uploaded. Default is same directory as script.

Example:
$uploaddir = "files/"; //set this to where your files should be uploaded. Default is same directory as script.
NIck
May 29, 2009 12:59pm
So anyway way to get this going if running Server in CGI Mode? apc_fetch() just returns false...
Alastair Battrick
June 12, 2009 7:56am
Brilliant, thankyou
chaitra
July 3, 2009 6:03am
Dear Sir,

I am trying for image displying loading status bar using ur progress.php script and I have installed and configured php_apc in my php.ini file. Also I have enabled apc.rfc1867 = on in php.ini file. But when I executed this file it will show uploading file window but progress bar is not moving and file uploading is not done.

It is showing error like 'rate' is null or not an object.
Please reply me soon.
Thanks in advance

Regards,
Chaitra
Edward
July 28, 2011 1:50pm
Is there any way to have the files auto-uploaded as selected? See I am using a unique id for the form submittal sent along with the file(well I will be) so that the data can be sent after the files are uploaded...then the id of the final form is compared to the files and ffmpeg queue is initiated. Kind of like gmail's upload feature...files are auto-uploaded without using a submit button...thats what I am looking to do
Karthik
August 13, 2009 12:27pm
The uploader seems to work but only shows 0% and 100%?
Try to upload a bigger file and it will say at 0% for a wile. Then it goes to 100%.
Karthik
August 13, 2009 12:29pm
How do i get the intermediate % values?
Ertem
September 4, 2009 2:58am
Works without any problem.I tested locally,uploaded about 90-100mb without any problem.
But i want to show user the uploaded file s name,how can i do this?
Ertem
September 4, 2009 8:06am
i solved previous question.

Now i want to post $uploaddir;$filename; variables to upload.php and show user the uploaded file name and directory using upload.php

I used action="upload.php" but variables on progress.php aren t sent to upload.php.

Any solution?
Alex
September 8, 2009 4:55pm
Is there any reason this would not work 'as is' in firefox 3? I have it working perfectly in IE, but I get exactly the same issue as Subeesh:
" I have tried this out, but the problem is, after pressing the submit button, on a fraction of a second the progres bar seems displayed, but after that, it displays some text like

{"total":9372,"current":9372,"rate":74976,"filename":"exchg_myoffers_main.tpl.html","name":"file","temp_filename":"\/tmp\/php1663g0","cancel_upload":0,"done":1}" Really like the simplicity of this but this one bit is killing me....

Appreciate some ideas.
Thanks

Alex.
EHCanadian
October 3, 2009 11:35am
Can you confirm this will work with the latest verion of yui 2.8.x as I wasn't able to out of the box
Martin
October 19, 2009 11:14am
Hi John,

i am on shared hosting and have run phpinfo. What am i looking for to see if APC is installed?

as i have copied your script, the progress bar pops up but nothing animates and doesn't upload the file.

thanks
Arun Kumar
December 14, 2009 12:47pm
the apc_fetch always returns false during the file upload but returns the array value only once the upload is get done. Any idea?
Runtime Settings
apc.cache_by_default 1
apc.coredump_unmap 0
apc.enable_cli 1
apc.enabled 1
apc.file_update_protection 0
apc.filters *
apc.gc_ttl 3600
apc.include_once_override 0
apc.max_file_size 1M
apc.mmap_file_mask
apc.num_files_hint 1000
apc.report_autofilter 0
apc.rfc1867 1
apc.rfc1867_freq 10k
apc.rfc1867_name APC_UPLOAD_PROGRESS
apc.rfc1867_prefix upload_
apc.shm_segments 1
apc.shm_size 30
apc.slam_defense 0
apc.stat 1
apc.stat_ctime 0
apc.ttl 7200
apc.user_entries_hint 4096
apc.user_ttl 7200
apc.write_lock 1
STAR68
October 25, 2009 6:04am
Thank you! It's excellent code!

But I have 3 question:
1. How to update current Rate upload?
2. How to Stop Uploading (a button to stop upload)
3. How to Seperate each file in each upload (not include all file in upload progress bar)
Webmaster
February 10, 2010 7:19am
I integrated a new (better) PHP Upload Progress Bar Script in my PHP project.
But script shows %Nan instead of actual percentage. I have installed all server settings properly.

Please help me, how to fix this problem?

Thanks in advance.
Dave JC
July 20, 2010 5:50am
Hello,

Greetings of the day...!!

However, I have run your code in Local Server Wamp, I face below issues.

When i am uploading a file , all time during uploading progress bar does not move. But after some time file uploads successfully. How?

I have APC installed as well as YUI library.

Kindly co-operate me to run this code.

Thank you,

With kind regards,

Dave JC
Mario
August 2, 2010 8:21am
hey there,

I got similar issue as DAVE with running script, i dont see progress bar. I have installed everything properly - Php apc is ok. After downloading i goto only string:

{"total":2047000,"current":2047000,"filename":"eng.mp3","name":"file","done":1,"start_time":1280755391.5058}

In firebug in firefox is an error during pushing the button submit: Break on ErrorKopiuj
YAHOO is not defined
(61 line: YAHOO.util.Connect.setForm(formName,true); )

Any ideas to resolve ?
Thanks in advance
MT Dew
September 3, 2010 4:06am
I'm having the same experiences as others with the progress bar. It starts from 0 during upload, but will Not animate to 100% in the process... However, It will jump to 100% after the file is done. I tried it with a large file and experienced it as well.

My YUI files is the same and im demoing the progress.php file. Does anyone have the full APC file/configuration settings?

I followed what was mentioned and made sure everything was set properly. Just an animation problem with the progress bar. everything else is working fine.

Any help will be great!
MT Dew
September 3, 2010 4:36am
John,

In regards to my progress bar animation problem. Could you please post your settings of the APC that you have in your php.ini file?

Even, though I followed what was posted. It could be possible i'm missing something, and sense your progress bar animates. I would like to compare the APC settings.

Sincerely,

MT.Dew
family health insurance rates
November 4, 2010 3:57pm
http://www.purplepuppetcompany.com/ cheap lexapro [url=http://www.purplepuppetcompany.com/] cheap lexapro [/url] http://www.fansofheroes.com/ zoloft [url=http://www.fansofheroes.com/] zoloft [/url] http://www.eleanorbaird.com/ doxycycline [url=http://www.eleanorbaird.com/] doxycycline [/url] http://www.eleanorbaird.com/ doxycycline online [url=http://www.eleanorbaird.com/] doxycycline online [/url] http://www.eleanorbaird.com/ buy doxycycline [url=http://www.eleanorbaird.com/] buy doxycycline [/url] http://www.eleanorbaird.com/ order doxycycline [url=http://www.eleanorbaird.com/] order doxycycline [/url] http://www.niueboro.com/ levitra online [url=http://www.niueboro.com/] levitra online [/url] http://www.notrock.com/ cheap health insurance [url=http://www.notrock.com/] cheap health insurance [/url] 58955@gmail.com
company health insurance
November 4, 2010 4:00pm
http://www.edmedicationsonline.net/ buy viagra [url=http://www.edmedicationsonline.net/] buy viagra [/url] http://www.edmedicationsonline.net/ levitra on line [url=http://www.edmedicationsonline.net/] levitra on line [/url] http://www.edmedicationsonline.net/ levitra online [url=http://www.edmedicationsonline.net/] levitra online [/url] http://www.edmedicationsonline.net/ cialis [url=http://www.edmedicationsonline.net/] cialis [/url] http://www.edmedicationsonline.net/ buy levitra online [url=http://www.edmedicationsonline.net/] buy levitra online [/url] http://www.edmedicationsonline.net/ viagra sale [url=http://www.edmedicationsonline.net/] viagra sale [/url] http://www.niueboro.com/ levitra online [url=http://www.niueboro.com/] levitra online [/url] http://www.notrock.com/ cheap health insurance [url=http://www.notrock.com/] cheap health insurance [/url] 49040@gmail.com


Your comment has been posted....