HTTP_Request
[ class tree: HTTP_Request ] [ index: HTTP_Request ] [ all elements ]

Source for file download-progress.php

Documentation is available at download-progress.php

  1. <?php
  2. /**
  3.  * An example of Listener usage with HTTP_Request. This downloads and saves
  4.  * the file displaying the progress bar in the process.
  5.  * 
  6.  * Note two things:
  7.  * 1) The file should be run in console, not in browser;
  8.  * 2) You should turn output buffering OFF for this to work properly.
  9.  * 
  10.  * $Id: download-progress.php,v 1.1 2003/10/27 10:48:49 avb Exp $
  11.  */
  12.  
  13. require_once 'HTTP/Request.php';
  14. require_once 'HTTP/Request/Listener.php';
  15. require_once 'Console/ProgressBar.php';
  16.  
  17. PEAR::setErrorHandling(PEAR_ERROR_DIE);
  18.  
  19.  
  20. {
  21.    /**
  22.     * Handle for the target file
  23.     * @var int 
  24.     */
  25.     var $_fp;
  26.  
  27.    /**
  28.     * Console_ProgressBar intance used to display the indicator
  29.     * @var object 
  30.     */
  31.     var $_bar;
  32.  
  33.    /**
  34.     * Name of the target file
  35.     * @var string 
  36.     */
  37.     var $_target;
  38.  
  39.    /**
  40.     * Number of bytes received so far
  41.     * @var int 
  42.     */
  43.     var $_size = 0;
  44.  
  45.     function HTTP_Request_DownloadListener()
  46.     {
  47.         $this->HTTP_Request_Listener();
  48.     }
  49.  
  50.    /**
  51.     * Opens the target file
  52.     * @param string Target file name
  53.     * @throws PEAR_Error
  54.     */
  55.     function setTarget($target)
  56.     {
  57.         $this->_target $target;
  58.         $this->_fp @fopen($target'wb');
  59.         if (!$this->_fp{
  60.             PEAR::raiseError("Cannot open '{$target}'");
  61.         }
  62.     }
  63.  
  64.     function update(&$subject$event$data = null)
  65.     {
  66.         switch ($event{
  67.             case 'sentRequest'
  68.                 $this->_target basename($subject->_url->path);
  69.                 break;
  70.  
  71.             case 'gotHeaders':
  72.                 if (isset($data['content-disposition']&&
  73.                     preg_match('/filename="([^"]+)"/'$data['content-disposition']$matches)) {
  74.  
  75.                     $this->setTarget(basename($matches[1]));
  76.                 else {
  77.                     $this->setTarget($this->_target);
  78.                 }
  79.                 $this->_bar =new Console_ProgressBar(
  80.                     '* ' $this->_target ' %fraction% KB [%bar%] %percent%''=>''-'
  81.                     79(isset($data['content-length'])round($data['content-length'/ 1024): 100)
  82.                 );
  83.                 $this->_size = 0;
  84.                 break;
  85.  
  86.             case 'tick':
  87.                 $this->_size += strlen($data);
  88.                 $this->_bar->update(round($this->_size / 1024));
  89.                 fwrite($this->_fp$data);
  90.                 break;
  91.  
  92.             case 'gotBody':
  93.                 fclose($this->_fp);
  94.                 break;
  95.  
  96.             default:
  97.                 PEAR::raiseError("Unhandled event '{$event}'");
  98.         // switch
  99.     }
  100. }
  101.  
  102. // Try using any other package if you like, but choose the bigger ones
  103. // to be able to see the progress bar
  104. $url 'http://pear.php.net/get/HTML_QuickForm-stable';
  105.  
  106. $req =new HTTP_Request($url);
  107.  
  108. $download =new HTTP_Request_DownloadListener();
  109. $req->attach($download);
  110. $req->sendRequest(false);
  111. ?>

Documentation generated on Mon, 11 Mar 2019 14:10:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.