By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,582 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Image upload with progress bar or please wait screen.

AaronL
P: 99
Hello,

I wrote a program that uploads images to a website using PHP but I'm not liking
how it's currently working.

When the user hits submit, the browser takes a long time, which I know is normal
because it's uploading images, however, I need to write a way to make it show
a progress bar, or at least say "uploading images please wait..."

I was thinking I would make imageuploader.php and then pass a $_GET value
with the progress with all of the files posted hidden, after the user chooses
which files to upload. I figured I'd have it upload one file at a time, and use the
header() function to move it to the next step and upload the 2nd file, but I realized
that wouldn't work because header would give me that "header already created"
type error.

The software I'm writing (hopefully) will be very professional and popular, but I
don't want my image uploader to be crap.

Any advice out there??
Apr 28 '10 #1
Share this Question
Share on Google+
5 Replies


code green
Expert 100+
P: 1,726
I have noticed a few people looking for a 'real' time progress bar but it seems to be difficult.
You can perform a simple cheat and use a moving-gif in the HTML to make the user feel comfortable.
Or there are a few Javascript classes that simulate a progress bar.
xp_progress.js is one I use.
Apr 28 '10 #2

Atli
Expert 5K+
P: 5,058
This is one of those major failures all the browser manufacturers share in. The displaying of the progress of a file upload should of course be the responsibility of the browser itself. We should not need to resort to some JavaScript hacks or static (read: fake) animated images to make it "appear" as if there is actually some progress display going on.

But hey, it's not like we're not used to covering for the shortcomings of the browsers... (Some more than others.)

On a more practical note, to actually display the status of a file upload, you would need a HTTP server module that is capable of relaying the status back to the browser as the data comes in. And of course the client-side code to receive and display the data. -- Not exactly something you just slap together for the sake of it.

A better approach is to just display a "loading" image, with a "Please wait..." header, that lets the user know something is happening, even though the progress of what is going on is nowhere to be seen.
Apr 28 '10 #3

AaronL
P: 99
Ok, well what I'd like to do is bring up the upload screen where you browse for files, and when
you click on the upload button, it brings it up to another screen that says please wait, then after they're done uploading, I want it to return to another page.

How can I do that because if I submit to the next page from the browse for files page, and then bring up the please wait page, if I use the header() command to redirect after the upload, it will give me the header already created message or something.

I'm most likely thinking of the wrong methods to do this.
May 1 '10 #4

chathura86
100+
P: 227
go through this manual, i managed to create a real time upload progress bar with this module (with the help of ajax).

http://php.net/manual/en/book.apc.php

but the downside is most hosting servers does not support this extension yet
and i did not found a windows version of the module.
so make sure of that. other than that it works perfectly

Regards
May 1 '10 #5

Atli
Expert 5K+
P: 5,058
@chelf
That's not something you can do with PHP alone. The PHP code is only executed after the request has been completely received (including the files). What you need to do is have JavaScript display the "Please wait" message when you click on the upload button, before the form is submitted.

The simples way to do that is to attach a function to the form's submit event that changes the contents of the page and the returns true.
Expand|Select|Wrap|Line Numbers
  1. <form action="process.php" onsubmit="showLoading()">
And then have the showLoading function create the elements for the "Please wait" message. (I copied this from an old project of mine.)
Expand|Select|Wrap|Line Numbers
  1. function showLoading() {
  2.     // Create a white box to cover the page.
  3.     var back = document.createElement('div');
  4.     back.style.backgroundColor = '#ffffff';
  5.     back.style.position = 'fixed';
  6.     back.style.zIndex = '0';
  7.     back.style.left = '0px';
  8.     back.style.top = '0px';
  9.     back.style.right = '0px';
  10.     back.style.bottom = '0px';
  11.     document.body.appendChild(back);
  12.  
  13.     // Add a box to contain the message.
  14.     var box = document.createElement('div');
  15.     box.style.position = 'absolute';
  16.     box.style.zIndex = '1';
  17.     box.style.width = '250px';
  18.     box.style.margin = '15px ' + ((document.body.offsetWidth / 2) - (250 / 2)) + 'px';
  19.     box.style.fontFamily = 'Verdana, Arial, serif';
  20.     document.body.appendChild(box);
  21.  
  22.     // Add the "Please wait" header
  23.     var message = document.createElement('span');
  24.     message.id = 'loading_header';
  25.     message.style.display = 'block';
  26.     message.style.fontSize = '175%';
  27.     message.style.fontWeight = 'bold';
  28.     message.style.textAlign = 'center';
  29.     message.innerHTML = 'Please wait';
  30.     box.appendChild(message);
  31.  
  32.     // Add the subheader message
  33.     var message = document.createElement('span');
  34.     message.id = 'loading_message';
  35.     message.style.display = 'block';
  36.     message.style.fontSize = '125%';
  37.     message.style.textAlign = 'center';
  38.     message.innerHTML = 'Your files are being uploaded.';
  39.     box.appendChild(message);
  40.  
  41.     // Add a loading image.
  42.     var img = document.createElement('img');
  43.     img.setAttribute('src', './loading.gif');
  44.     img.style.display = 'block';
  45.     img.style.width = '64px';
  46.     img.style.height = '64px';
  47.     img.style.margin = '15px auto';
  48.     box.appendChild(img);
  49. }
The process.php code can then process the files and redirect to whatever page you want displayed when the upload is complete. If you keep getting a "Headers already sent" message when trying to redirect, read this article.
May 1 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.