Examples TOCexamples

Preload Shockwave Flash object

$Date: 2005/07/25 11:23:37 $

 Table of contents

Introduction

This example requires :

Be aware that:


This example will display a basic progress bar while retrieving a large file.

We have created a Request/Listener class HTTP_Request_DownloadListener on lines 18 to 104. In this Listener, a progress bar was integrated on lines 78 to 84. It's a basic progress bar with only one render option changed (animation speed).

On lines 90 and 92 the progress bar is updated depending of the data size already received.

Media file is identify by URL defined at line 106.

When the shockwave flash object is fully loaded, page sw4p.html is shown on lines 115 to 117.

[Top]

 Render options

Animation speed is set to 10 (see source at line 79).

[Top]

 PHP source syntax highlight

  1. <?php
  2. /**
  3.  * An example of Listener usage with HTTP_Request and HTML_Progress.
  4.  *
  5.  * Credit: Alexey Borzov <avb@php.net>
  6.  *         for his download-progress.php pattern in HTTP_Request package
  7.  *
  8.  * @version    $Id: shockwaveflash.php,v 1.2 2005/07/25 11:23:37 farell Exp $
  9.  * @author     Laurent Laville <pear@laurent-laville.org>
  10.  * @package    HTML_Progress
  11.  * @subpackage Examples
  12.  */
  13.  
  14. require_once 'HTTP/Request.php';
  15. require_once 'HTTP/Request/Listener.php';
  16. require_once 'HTML/Progress.php';
  17.  
  18. class HTTP_Request_DownloadListener extends HTTP_Request_Listener
  19. {
  20.    /**
  21.     * Handle for the target file
  22.     * @var int
  23.     */
  24.     var $_fp;
  25.  
  26.    /**
  27.     * ProgressBar intance used to display the indicator
  28.     * @var object
  29.     */
  30.     var $_bar;
  31.  
  32.    /**
  33.     * Name of the target file
  34.     * @var string
  35.     */
  36.     var $_target;
  37.  
  38.    /**
  39.     * Number of bytes received so far
  40.     * @var int
  41.     */
  42.     var $_size = 0;
  43.  
  44.     function HTTP_Request_DownloadListener()
  45.     {
  46.         $this->HTTP_Request_Listener();
  47.     }
  48.  
  49.    /**
  50.     * Opens the target file
  51.     * @param string Target file name
  52.     * @throws PEAR_Error
  53.     */
  54.     function setTarget($target)
  55.     {
  56.         $this->_target = $target;
  57.         $this->_fp = @fopen($target, 'wb');
  58.         if (!$this->_fp) {
  59.             PEAR::raiseError("Cannot open '{$target}'");
  60.         }
  61.     }
  62.  
  63.     function update(&$subject, $event, $data = null)
  64.     {
  65.         switch ($event) {
  66.             case 'sentRequest':
  67.                 $this->_target = basename($subject->_url->path);
  68.                 break;
  69.  
  70.             case 'gotHeaders':
  71.                 if (isset($data['content-disposition']) &&
  72.                     preg_match('/filename="([^"]+)"/', $data['content-disposition'], $matches)) {
  73.  
  74.                     $this->setTarget(basename($matches[1]));
  75.                 } else {
  76.                     $this->setTarget($this->_target);
  77.                 }
  78.                 $this->_bar =& new HTML_Progress();
  79.                 $this->_bar->setAnimSpeed(10);
  80.                 $inc = isset($data['content-length'])? round($data['content-length'] / 100) : 1;
  81.                 $this->_bar->setIncrement(intval($inc));
  82.                 echo '<style type="text/css">'.$this->_bar->getStyle().'</style>';
  83.                 echo '<script type="text/javascript">'.$this->_bar->getScript().'</script>';
  84.                 echo $this->_bar->toHtml();
  85.                 $this->_size = 0;
  86.                 break;
  87.  
  88.             case 'tick':
  89.                 $this->_size += strlen($data);
  90.                 $this->_bar->display();
  91.                 $val = round($this->_size / $this->_bar->getIncrement());
  92.                 $this->_bar->setValue(intval($val));
  93.                 fwrite($this->_fp, $data);
  94.                 break;
  95.  
  96.             case 'gotBody':
  97.                 fclose($this->_fp);
  98.                 break;
  99.  
  100.             default:
  101.                 PEAR::raiseError("Unhandled event '{$event}'");
  102.         } // switch
  103.     }
  104. }
  105.  
  106. $url = 'http://pear.laurent-laville.org/HTML_Progress/examples/viewlet/sw4p.swf';
  107.  
  108. $req =& new HTTP_Request($url);
  109.  
  110. $download =& new HTTP_Request_DownloadListener();
  111. $req->attach($download);
  112. $req->sendRequest(false);
  113.  
  114.  
  115. $href = 'http://'.$_SERVER['SERVER_NAME']. dirname($_SERVER['PHP_SELF']) . '/sw4p.html';
  116. $go = '<script type="text/javascript">window.location.href="'.$href.'";</script>';
  117. echo $go;
  118. ?>

[Top]