Implementing file upload progress with Zend

2019-03-31 08:52发布

问题:

Hi Im attempting to implement upload progress with Zend however I havent found any detailed tutorials. A pointer would be useful.

Thanks.

回答1:

I don't remember having ever seen a fully-detailled tutorial explaining how to get a progress bar for uploads with Zend Framework ; but here are a few pointers, that should guide you a bit, provided you already know so stuff about PHP, file uploads, and Zend Framework...

First of all, you'll need one of those two PHP extension installed :

  • either APC
  • or uploadprogress

Which mean you'll be able to get that kind of progress bar only if you are admin of your server (those kind of extensions are generally not installed by default -- and not on shared hosting)


Then, you have to use some special "hidden" fields in your upload form ; about that, you can take a look at the configuration options of APC ; especially those related to RFC 1867.

If you are using Zend Framework, I suppose you are already using some Zend_Form_Element_File in your form. It should already do what is necessary about those fields -- you'd better check that, to be sure, btw.


Now that you form is OK, you can finally take a look at Zend_ProgressBar, and at the documentation chapter that describes Progress for file uploads

Your code will probably look a bit like this (quoting the doc) :

$adapter = new Zend_ProgressBar_Adapter_Console();
$upload  = Zend_File_Transfer_Adapter_Http::getProgress($adapter);

$upload = null;
while (!$upload['done']) {
    $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
}

And, to fetch that information regularly, you'll have to do some polling from the web page, using some kind of Ajax requests.


About the uploadprogress extension, you can take a look at these articles :

  • Upload Progress Meter - Common issues and some answers -- it might give you a few pointers.
  • AJAX File upload Progress and PHP AJAX File Upload Progress Meter Updates
  • Upload Progress Meter extension for PHP 5.2

Those posts are not specifically targetting Zend Framework, but might give you an idea of what is going on ;-)


BTW, you'll probably want to test all that on your local machine, which is easier to develop... And that will mean that file upload will really be fast ; which is not quite good to test any kind of progress upload indicator...

About that, you might be interesting in "slowing down" your local network interface ; those might help :

  • Network tools that simulate slow network connection
  • Firefox plugin to simulate slow internet connection?


Hope this helps at least a bit ;-)

And, while you're at it : why don't you write down your findings into some kind of nice and detailled tutorial ? That might be useful to some people ;-)