16 Apr
Posted by Harper as Technology at 05:21 PM
Tags: ajax-uploads, pecl, php, php-progress-bar, progress-bar, progressbar, prototype, prototype-uploads, Technology, uploadprogress, uploads, yui
Today I decided I was going to build the asynchronous picture uploader for one of our new projects. I had installed the PECL Uploadprogress extension awhile ago and I just needed to build an example and then build it into our app.
However, my plan was spited by the opensource gods who delivered very little documentation about how to use the PECL Uploadprogress extension. So here is my quick writeup and example.
After crawling for 30 minutes through the C++ source of the Uploadprogress extension, I figured out what the heck was actually happening. The basic set of events is that an upload occurs. with that upload post is a hidden input that has a unique ID in it. The Uploadprogress extension then assigns the progress of that upload to that ID. Then when you call the uploadprogress_get_info() function with that unique id, the response is the upload progress. So, the two key pieces of information are this:
The function that accesses the upload progress is:
uploadprogress_get_info($unique_id);
It takes a string that is the uploads unique id. To set this id you include the following hidden input in your upload form:
<input type=”hidden” name=”UPLOAD_IDENTIFIER” value=”<?php echo $some_uniq_id; ?>” />
This sets the ID of the upload that the uploadprogress extension then can track. THIS IS VERY IMPORTANT. As far as I could tell, if you entered a different ID name, your uploadprogress_get_info($unique_id); will not work. If there is no upload, or it is complete uploadprogress_get_info() returns not a array.
So the pseudo php code is like this
upload.php
<form crap>
<input type=”hidden” name=”UPLOAD_IDENTIFIER” value=”<?php echo $some_uniq_id; ?>” />
<input type=”file” ……. />
<input type=”submit” …. />
</form>
progress.php
<?
$uniq_id = $_GET["id"];
print_r(uploadprogress_get_info($unique_id));
?>
so you would load up upload.php and upload a huge file. Then in another browser window you would view progress.php?id=$some_uniq_id and theoretically you would get a raw progress response from uploadprogress_get_info().
heh.
So I have created a working (at least on my server) example of this for yall to test. I have placed it in my code archive under php_upload_progressbar. This example uses prototype and YUI components to achieve totally asynchronous uploads and then has a CSS progress bar to handle the upload progress. The upload is through an Iframe using the YUI container.js and the updaters are using prototype. I imagine I could have used just YUI, but I know prototype better and it was easy to rock out in about thirty seconds. I have attempted to thoroughly comment both my progress.php and my upload.php so it should be somewhat clear on what is going on.
Let me know if you have questions or comments. I haven’t placed a live example anywhere because I don’t really feel comfortable having crazy people uploading random porn to my servers. however, I would love to help peeps that are having issues getting the example to run.
[tags]uploads, ajax uploads, uploadprogress, pecl, prototype, php, yui, prototype uploads, progress bar, php progress bar, progressbar[/tags]
8 Responses
Fredrik Davidsson
July 25th, 2007 at 11:44 am
1Hi,
Thanks for the information!
While doing some tests I placed the UPLOAD_IDENTIFIER tag after the file tag and that doesn’t work.
It might help others if point that out.
Have a nice day.
Regards Fredrik
Sweden
Sid
August 7th, 2007 at 1:18 am
2Thanks for the example, I understand it quite a bit more now. However I still can’t get it to run. I think I have installed the extension correctly but uploadprogress_get_info($unique_id) just doesn’t seem to be returning anything. Is there a way to verify that the extension is installed and working? My php version 5.2.1 (Ubuntu 7.04, apache 2) so that should be ok. And when I viewed phpinfo, I could see the extension listed there. Any idea?
Sid
August 7th, 2007 at 1:23 am
3Hey, I got it to work with a little bit tweaking. Cheers!
David
August 14th, 2007 at 12:15 pm
4Sid - any chance you could let us know what tweaking you did, as I’m having a similar issue. Cheers.
miccio
December 4th, 2007 at 9:47 am
5links to examples are broken..
Whisky
January 24th, 2008 at 11:40 am
6I’ve got the same problem Sid had in post No. 2. Does anybody have a solution?
Bill
January 29th, 2008 at 5:19 am
7Be sure you change the example in progress.php so that $uniq_id is actually $unique_id, or at least so the variables are the same. After I changed that it worked fine.
Will Mason
February 13th, 2008 at 3:14 pm
8I dot this thing working with a little tweaking, and the damn thing is brilliant. Here is my implementation. I find it simple, but thats because it fits into my custom MVC. Please note I am using jQuery for client-side DOM manipulation, and xAjax for Async requests (upload progress).
MARKUP (upload.html):
<input name=”MAX_FILE_SIZE” id=”MAX_FILE_SIZE” value=”" type=”hidden” />
<input name=”UPLOAD_IDENTIFIER” id=”UPLOAD_IDENTIFIER” value=”" type=”hidden” />
UPLOAD PROCESSOR (processUpload.php):
window.parent.fileUpload.finish();
JS HELPER (fileUpload.js):
$(function(){
fileUpload.init();
});
var fileUpload = {
timer: null,
timeout: 800,
iFrame: null,
init: function (){
$.xajaxLoad({bind: false});
},
start: function (el){
$.xajaxLoad.show();
this.checkProgress();
this.iFrame = $(’#’ + el.form.target);
el.form.submit();
},
checkProgress: function (){
var fUp = this;
if ($(’#xajaxLoad’).size() == 0)
return false;
if ($(’#uploadProgress’).size() == 0)
{
$(’#xajaxLoad’).append(
”
+ ”
+ ‘0%’
// + ‘0kb/s’
+ ‘* Seconds Left’
+ ‘Cancel‘
+ ”
);
$(’#UP_cancel’).click(function(e){
e.preventDefault();
fUp.iFrame.attr(’src’, ‘about:blank’);
clearTimeout(fUp.timer);
$.xajaxLoad.hide();
});
}
xDoUpdateBytesSent({
// avgSpeed: ‘UP_avgSpeed’,
timeLeft: ‘UP_timeLeft’,
percent: ‘UP_percent’,
percentBar: ‘UP_percentBar’,
id: $(’#UPLOAD_IDENTIFIER’).val()
});
this.timer = setTimeout(’fileUpload.checkProgress()’, this.timeout);
},
finish: function (){
clearTimeout(this.timer);
$.xajaxLoad.hide();
alertSaved();
}
};
XAJAX PHP FUNCTION:
public function xDoUpdateBytesSent()
{
$info = uploadprogress_get_info($this->getParam(”id”));
if ($info)
{
if (($asDiv = $this->getParam(”avgSpeed”)))
{
$tmp = wmFileSystem::bytesToHuman($info['speed_average']);
$this->xajaxResponse->addAssign($asDiv, “innerHTML”, $tmp.”/S”);
}
if (($tlDiv = $this->getParam(”timeLeft”)))
{
$this->xajaxResponse->addAssign($tlDiv, “innerHTML”, $info['est_sec'].” Seconds Left”);
}
if ($this->getParam(”percent”) || $this->getParam(”percentBar”))
{
$pDiv = $this->getParam(”percent”);
$pbDiv = $this->getParam(”percentBar”);
$tmp = floor($info['bytes_uploaded'] / $info['bytes_total'] * 100);
if ($pDiv)
$this->xajaxResponse->addAssign($pDiv, “innerHTML”, $tmp.”%”);
if ($pbDiv)
$this->xajaxResponse->addScript(”$(’#”.$pbDiv.”‘).css({width: ‘”.$tmp.”%’});”);
}
}
return $this->xajaxResponse;
}
RSS feed for comments on this post · TrackBack URI
Leave a reply
Be sure and take a gander at my photos.
If you want to contact me click here to start a chat.
Status
Pictures
Friends
Popular Tags
action awesome blogs books bush cell chicago chris colorado crobar dylan family food games google hacking harper hiromi humor india insane internet iraq job juggle juggling matiss metal movies music nokia phone php politic reed rock school search server sick Sites Technology travel video war
search
Categories
Archives
Recent Entries
Recent Comments
Most Commented
Nata2.org is © 1997-2008 Harper Reed. Theme stoled and inspired by the great BloggingPro theme by: Design Disease