Upload Progress Bar??? HELP!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Upload Progress Bar??? HELP!!!

Post by JustinMs66 »

hey so i have a working PHP code for a upload form, but i wana make a progress bar for it. so like if it's a pretty big file, people will know how much longer it'l take. here is my current code:

Code: Select all

<?php
$bad_types = array('application/octet-stream','application/x-msdos-program','application/x');
if( in_array( $_FILES['uploadedfile']['type'], $bad_types ) )
{
    echo "That File type is not supported.";
}
else
{
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']).
        " has been uploaded. here is the link to your file: <a href=uploads/".  basename( $_FILES['uploadedfile']['name']). ">".  basename( $_FILES['uploadedfile']['name'])."</a>";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

We've talked about progress bars before. There is nothing native that will do them. Perl can do them as can Flash, Java and ActiveX controls. PHP, by itself, cannot do them.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

If move_uploaded_file() could take the source file as a small chunk and we could loop till the file size and pass the small chunk to the function and display the progress bar. But move_uploaded_file() function takes the source files and do it all and return just a boolean value. I think php.net community should make the function parameters customizable .

Cheers,
Dibyendra
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

PHP processes on the server. Status bars report on the client. Unless you plan on sending requests to the server during different stages of the upload (which I don't think is possible) you are going to have to use a client side solution. This is probably a job for AJAX or some other DHTML type of technology.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

dibyendrah wrote:If move_uploaded_file() could take the source file as a small chunk[...]
move_uploaded_file already works on the temporary file already created on the server's local filesystem, i.e. the actual upload is finished when move_uploaded_file is invoked.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I always throw this solution out there just because it's easy... you could just display an animated gif that says "Loading..." and maybe some sort of loading-type animation. This way at least the user sees that SOMETHING is happening.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

The Ninja Space Toad wrote:This way at least the user sees that SOMETHING is happening.
ok and what if the upload fails for some reason... then what do you show to the user :lol:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

n00b Saibot wrote:
The Ninja Space Toad wrote:This way at least the user sees that SOMETHING is happening.
ok and what if the upload fails for some reason... then what do you show to the user :lol:
umm...

"Sorry... upload failed."

:?: :?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

I posted that as a follow-up joke on your line. but seriously if you ask me, then I would change the line a bit, may be to 'Please wait while your file is being uploaded...' and animating something beyond that to achieve the same effect you were talking about. Afterwards, if loading fails then you can show the error to the user — "Your File was not uploaded because <insert reason here>".

I fear we are straying off-topic...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd wrote:We've talked about progress bars before. There is nothing native that will do them. Perl can do them as can Flash, Java and ActiveX controls. PHP, by itself, cannot do them.
This is about as good an explanation as you can expect. Several of us have posted on this subject before. PHP will not take care of status bars. AJAX can, JavaScript can, client-side technologies can. Server-side technologies will not do this for you.
travist349
Forum Newbie
Posts: 3
Joined: Fri Sep 08, 2006 3:16 pm

Post by travist349 »

Hey guys,

I noticed a lot of people wondering about how to integrate an upload progress bar.

I have implemented this in my site, and I used the TesUpload by Thomas Epineer. It is a fantastic script that works great!

It uses a colaboration of PHP, Perl, and AJAX to create an upload progress bar that does not require page refreshes.

Let me know if you guys have any problems.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

feyd wrote:We've talked about progress bars before. There is nothing native that will do them. Perl can do them as can Flash, Java and ActiveX controls. PHP, by itself, cannot do them.
Perl?! Really how? What does Perl have that PHP doesn't!

Image: Smurfing ahead
Last edited by Ollie Saunders on Fri Sep 08, 2006 6:41 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

ole wrote:Really how? What does Perl have that PHP doesn't!
I'm interested in know that as well... if I recall it was something about hooks?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ole wrote:Perl?! Really how? What does Perl have that PHP doesn't!
Uh ... Larry Wall ... and the ability to monitor uploading files.

The reason it cannot be done in PHP is that PHP is currently unable to comminicate to the submitting script any sort of handle or identifier to access the temporary file. It therefor cannot monitor the upload or pass the info to a monitoring script. With PHP you have to wait until the receiving script runs to check the uploaded file -- and by that time the file is already uploaded.

There was/is a patch for PHP that adds this feature. There are also a long running series of requests and patches to add this functionality to PHP -- one by the author of that patch. None have been accepted so far that I know of. I have not followed it for a while so do know if there are any plans in 6.0 for this.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

what does scruvy mean? :lol:
Post Reply