home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / download-progress.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  2.9 KB  |  114 lines

  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. set_time_limit(0);
  20.  
  21. class HTTP_Request_DownloadListener extends HTTP_Request_Listener
  22. {
  23.    /**
  24.     * Handle for the target file
  25.     * @var int
  26.     */
  27.     var $_fp;
  28.  
  29.    /**
  30.     * Console_ProgressBar intance used to display the indicator
  31.     * @var object
  32.     */
  33.     var $_bar;
  34.  
  35.    /**
  36.     * Name of the target file
  37.     * @var string
  38.     */
  39.     var $_target;
  40.  
  41.    /**
  42.     * Number of bytes received so far
  43.     * @var int
  44.     */
  45.     var $_size = 0;
  46.  
  47.     function HTTP_Request_DownloadListener()
  48.     {
  49.         $this->HTTP_Request_Listener();
  50.     }
  51.  
  52.    /**
  53.     * Opens the target file
  54.     * @param string Target file name
  55.     * @throws PEAR_Error
  56.     */
  57.     function setTarget($target)
  58.     {
  59.         $this->_target = $target;
  60.         $this->_fp = @fopen($target, 'wb');
  61.         if (!$this->_fp) {
  62.             PEAR::raiseError("Cannot open '{$target}'");
  63.         }
  64.     }
  65.  
  66.     function update(&$subject, $event, $data = null)
  67.     {
  68.         switch ($event) {
  69.             case 'sentRequest': 
  70.                 $this->_target = basename($subject->_url->path);
  71.                 break;
  72.  
  73.             case 'gotHeaders':
  74.                 if (isset($data['content-disposition']) &&
  75.                     preg_match('/filename="([^"]+)"/', $data['content-disposition'], $matches)) {
  76.  
  77.                     $this->setTarget(basename($matches[1]));
  78.                 } else {
  79.                     $this->setTarget($this->_target);
  80.                 }
  81.                 $this->_bar =& new Console_ProgressBar(
  82.                     '* ' . $this->_target . ' %fraction% KB [%bar%] %percent%', '=>', '-', 
  83.                     79, (isset($data['content-length'])? round($data['content-length'] / 1024): 100)
  84.                 );
  85.                 $this->_size = 0;
  86.                 break;
  87.  
  88.             case 'tick':
  89.                 $this->_size += strlen($data);
  90.                 $this->_bar->update(round($this->_size / 1024));
  91.                 fwrite($this->_fp, $data);
  92.                 break;
  93.  
  94.             case 'gotBody':
  95.                 fclose($this->_fp);
  96.                 break;
  97.  
  98.             default:
  99.                 PEAR::raiseError("Unhandled event '{$event}'");
  100.         } // switch
  101.     }
  102. }
  103.  
  104. // Try using any other package if you like, but choose the bigger ones
  105. // to be able to see the progress bar
  106. $url = 'http://pear.php.net/get/HTML_QuickForm-stable';
  107.  
  108. $req =& new HTTP_Request($url);
  109.  
  110. $download =& new HTTP_Request_DownloadListener();
  111. $req->attach($download);
  112. $req->sendRequest(false);
  113. ?>
  114.