Shockwave Flash

Overview

This example will downloads and saves the shockwave flash object displaying the progress bar in the process.

Absolute URL of SWF movie to download is defined on lines 120-122.

Screenshot

Once download operation is completed and progress bar hidden, this example will display something like :

sample screenshot

... my presentation of an old and un-finished project, named SW4P (Setup Wizard for PHP) or also HTML_SetupWizard.

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains step by step

This example used a horizontal progress bar with all its default values.

Source Code

  1. <?php
  2. /**
  3. * An example of Listener usage with HTTP_Request and HTML_Progress2.
  4. * Will download and save the file[1] displaying the progress bar in the process.
  5. * [1] qarbon shockwave flash presentation of SW4P (411 Kb)
  6. *
  7. * Credit:     Alexey Borzov <avb@php.net>
  8. *             for his download-progress.php pattern in HTTP_Request package
  9. *
  10. * @version    $Id: swf.php,v 1.1 2005/06/12 21:07:53 farell Exp $
  11. * @author     Laurent Laville <pear@laurent-laville.org>
  12. * @package    HTML_Progress2
  13. * @subpackage Examples
  14. * @link       http://www.qarbon.com
  15. * @access     public
  16. */
  17. require_once 'HTTP/Request.php';
  18. require_once 'HTTP/Request/Listener.php';
  19. require_once 'HTML/Progress2.php';
  20.  
  21. PEAR::setErrorHandling(PEAR_ERROR_DIE);
  22.  
  23. class HTTP_Request_DownloadListener extends HTTP_Request_Listener
  24. {
  25.    /**
  26.     * Handle for the target file
  27.     * @var int
  28.     */
  29.     var $_fp;
  30.  
  31.    /**
  32.     * ProgressBar intance used to display the indicator
  33.     * @var object
  34.     */
  35.     var $_bar;
  36.  
  37.    /**
  38.     * Name of the target file
  39.     * @var string
  40.     */
  41.     var $_target;
  42.  
  43.    /**
  44.     * Number of bytes received so far
  45.     * @var int
  46.     */
  47.     var $_size = 0;
  48.  
  49.     function HTTP_Request_DownloadListener()
  50.     {
  51.         $this->HTTP_Request_Listener();
  52.     }
  53.  
  54.    /**
  55.     * Opens the target file
  56.     * @param string Target file name
  57.     * @throws PEAR_Error
  58.     */
  59.     function setTarget($target)
  60.     {
  61.         $this->_target = $target;
  62.         $this->_fp = @fopen($target, 'wb');
  63.         if (!$this->_fp) {
  64.             PEAR::raiseError("Cannot open ''");
  65.         }
  66.     }
  67.  
  68.     function update(&$subject, $event, $data = null)
  69.     {
  70.         switch ($event) {
  71.             case 'sentRequest':
  72.                 $this->_target = basename($subject->_url->path);
  73.                 break;
  74.  
  75.             case 'gotHeaders':
  76.                 if (isset($data['content-disposition']) &&
  77.                     preg_match('/filename="([^"]+)"/',
  78.                                $data['content-disposition'],
  79.                                $matches))
  80.                 {
  81.                     $this->setTarget(basename($matches[1]));
  82.                 } else {
  83.                     $this->setTarget($this->_target);
  84.                 }
  85.                 $this->_bar =& new HTML_Progress2();
  86.                 if (isset($data['content-length'])) {
  87.                     $inc = round($data['content-length'] / 100);
  88.                 } else {
  89.                     $inc = 1;
  90.                 }
  91.                 $this->_bar->setIncrement(intval($inc));
  92.                 echo '<style type="text/css">'
  93.                      . $this->_bar->getStyle()
  94.                      . '</style>';
  95.                 echo '<script type="text/javascript">'
  96.                      . $this->_bar->getScript()
  97.                      . '</script>';
  98.                 $this->_bar->display();
  99.                 $this->_size = 0;
  100.                 break;
  101.  
  102.             case 'tick':
  103.                 $this->_size += strlen($data);
  104.                 $val = round($this->_size / $this->_bar->getIncrement());
  105.                 $this->_bar->moveStep(intval($val));
  106.                 fwrite($this->_fp, $data);
  107.                 break;
  108.  
  109.             case 'gotBody':
  110.                 $this->_bar->hide();
  111.                 fclose($this->_fp);
  112.                 break;
  113.  
  114.             default:
  115.                 PEAR::raiseError("Unhandled event ''");
  116.         }
  117.     }
  118. }
  119.  
  120. $movie = 'sw4p.swf';
  121. $url   = 'http://pear.laurent-laville.org/HTML_Progress/examples/viewlet/'
  122.        . $movie;
  123.  
  124. $req =& new HTTP_Request($url);
  125.  
  126. $download =& new HTTP_Request_DownloadListener();
  127. $req->attach($download);
  128. $req->sendRequest(false);
  129.  
  130.  
  131. $codeBase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab'
  132.           . '#version=5,0,0,0';
  133. $pluginsPage = 'http://www.macromedia.com/shockwave/download/index.cgi'
  134.              . '?P1_Prod_Version=ShockwaveFlash';
  135. ?>
  136. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  137.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  138. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  139. <head>
  140. <title>Setup Wizard for PHP</title>
  141. <meta name="Keywords" content="viewlet, qarbon, sw4p, HTML_SetupWizard">
  142. <meta name="Description" content="a presentation to SW4P (Setup Wizard for PHP)">
  143. </head>
  144.  
  145. <body bgcolor="#ffffff">
  146.  
  147. <div align="center">
  148. <object
  149.   classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
  150.   codebase="<?php echo $codeBase; ?>"
  151.   width="530"
  152.   height="416">
  153.   <param name="movie" value="<?php echo $movie; ?>">
  154.   <param name="quality" value="high">
  155.   <param name="bgcolor" value="#ffffff">
  156.   <embed src="<?php echo $movie; ?>"
  157.       quality="high"
  158.       bgcolor="#ffffff"
  159.       width="530"
  160.       height="416"
  161.       type="application/x-shockwave-flash"
  162.       pluginspage="<?php echo $pluginsPage; ?>">
  163. </object>
  164. </div>
  165.  
  166. </body>
  167. </html>