Some useful tips on Flash, caching and Php

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:

[as] loadMovie(“clip.swf?123”, target);
[/as]

 

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

[as] clipname= “cache”+VERSION+”-clip.swf”;
loadMovie(clipname, target); //Loaded
//After the first load the movie is cached:
loadMovie(clipname, target2); //Cached
[/as] And use a .htaccess file (ModRewrite required):
[apache] RewriteEngine on
RewriteCond %{REQUEST_URI} cache([0-9]+)-(.*).swf(.*)$
RewriteRule ^cache([0-9]+)-(.*).swf(.*)$ $2.swf [L] [/apache]

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:

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

Be sure to set the correct content type:

$filename= "clip.swf";
$fp = @fopen($filename,"r");
if($fp){
	header('Last-Modified: '.date( "D, j M Y G:i:s GMT", filemtime($filename) ));
	header('Content-Length: '.filesize($filename));
	header('Connection: close');
	header('Content-Type: application/x-shockwave-flash');

	while (!feof($fp)){
		echo(fgets($fp, 4096));
		ob_flush();
	}
	fclose($fp);
}
else
{
	header("HTTP/1.0 404 Not Found");
}

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