Darryl Hein
Darryl Hein

Reputation: 144967

Creating a file progress bar in PHP

Does anyone know of any methods to create a file upload progress bar in PHP? I have often heard that it's impossible.

I have one idea, but not sure if it would work: have a normal file upload, but instead submit to an iframe. When this is submitted, store the file information (size and temp location) in the session. At the same time, start an AJAX call to every say 10 seconds to check the size of the file compared to the size stored in the session. This would return the size to the AJAX and then a progress bar would be sized and maybe display the uploaded size to the user.

Thoughts?

Upvotes: 5

Views: 8300

Answers (8)

Styx
Styx

Reputation: 1323

From PHP 5.4 it is in session extension: http://php.net//manual/pl/session.upload-progress.php

Upvotes: 1

SchizoDuckie
SchizoDuckie

Reputation: 9401

You will definately want to go with digitgerald's FancyUpload. It's Mootools & swfuplaod based, and it sports a nice queue with statusses, progress, eta etc. It's really the slickest method i've seen for uploading files. For my personal use case ivé used it to let the client select 1.2 gb of PDF files and upload them. Newer ones get renamed and versioned automatically, same are skipped, etc.

Upvotes: 0

Gaurav
Gaurav

Reputation: 486

You can try YUI or Prototype or JQuery

Upvotes: 3

davethegr8
davethegr8

Reputation: 11595

So far, the most common way of doing this is SWFUpload: http://www.swfupload.org/

However, it is possible with pure PHP, just very difficult and very experimental. I'll see if I can find the link.

Edit: According to comments on php.net, as of 5.2 there is a hook to handle upload progress. http://us.php.net/features.file-upload#71564

More explanation:

Rasmus' Example:

Upvotes: 5

Wilco
Wilco

Reputation: 33356

I'd recommend looking at SWFUpload to accomplish what you want. It's fairly flexible and supports queueing of files, so you could even handle multi-file uploads.

Upvotes: 0

davr
davr

Reputation: 19137

In my opinion, the best / easiest solution is to build a small flash widget, that consists of an 'Upload' button and a progress bar. Flash gives you very detailed feedback on how much data has been uploaded so far, and you can build a nice progress bar based on that. Doesn't require inefficient polling of the server, and in fact doesn't require any changes at all to your server code. Google for 'flash uploader' and you'll find many people have already written these widgets and are happy to sell them to you for a buck.

Upvotes: 0

Rexxars
Rexxars

Reputation: 1192

You're pretty much figured out how to do it. The main problem is you usually don't have access to the size of the uploaded file until it's done uploading.

There are workarounds for this: Enabling APC, you to access this information if you include a field called "APC_UPLOAD_PROGRESS" and use apc_fetch() for retrieving a cache entry with the status.

There's also a plugin called uploadprogress but it's not very well documented and doesn't work on Windows (last I checked anyway).

An alternative is to use Flash for doing it. See scripts like FancyUpload.

Before APC came along I had to write a CGI script in C that wrote information to a text file. APC seems like a much better way to do it now though.

Hope this helps.

Upvotes: 6

warren
warren

Reputation: 33435

In pure PHP, you are correct: it's not possible.

If you AJAX-ify this, then you could do what you're describing. The only progress meters I've ever seen are in Javascript or Flash, though I imagine Silverlight could do it also.

Upvotes: 0

Related Questions