Some useful tips on Flash, caching and Php

Posted in Flash, PHP at 12:48 am by Madarco  

Making the test for some strange flash cache behaviour I've learned some useful tips:

You can be sure a movie will be loaded appending a query string to the file name:

Actionscript:
  1. loadMovie("clip.swf?123", target);

 

You can force the user to reload a movie when you release a new version:

Actionscript:
  1. clipname= "cache"+VERSION+"-clip.swf";
  2. loadMovie(clipname, target); //Loaded
  3. //After the first load the movie is cached:
  4. loadMovie(clipname, target2); //Cached

And use a .htaccess file (ModRewrite required):

Apache:
  1. RewriteEngine on
  2. RewriteCond %{REQUEST_URI} cache([0-9]+)-(.*)\.swf(.*)$
  3. RewriteRule ^cache([0-9]+)-(.*)\.swf(.*)$ $2\.swf [L]

Only when you change the VERSION variable (pass it with flashVars) the movie will be reload, otherwise it will be loaded from the browser cache.
 

You can make cacheable a file downloaded through php:

PHP:
  1. header('Last-Modified: '.date( "D, j M Y G:i:s \G\M\T", filemtime($filename) ));

Be sure to set the correct content type:

PHP:
  1. $filename= "clip.swf";
  2. $fp = @fopen($filename,"r");
  3. if($fp){
  4.     header('Last-Modified: '.date( "D, j M Y G:i:s \G\M\T", filemtime($filename) ));
  5.     header('Content-Length: '.filesize($filename));
  6.     header('Connection: close');
  7.     header('Content-Type: application/x-shockwave-flash');
  8.  
  9.     while (!feof($fp)){
  10.         echo(fgets($fp, 4096));
  11.         ob_flush();
  12.     }
  13.     fclose($fp);
  14. }
  15. else
  16. {
  17.     header("HTTP/1.0 404 Not Found");
  18. }

Without Last-Modified or ETag headers flash doesn't cache the files.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.