Random Flash SWF Onload PHP Script

by Hiroshi on June 18, 2008

in Flash & PHP, Randomizing, Rotating Content, Scripts

Put all your swf files in a folder named as ‘images’ or whatever you name it (change folder name in script) and use this script to display a random flash movie, everytime page loads.

PHP Code

<?php
$imglist='';
//$img_folder is the variable that holds the path to the swf files.
// see that you dont forget about the "/" at the end
$img_folder = "images/";
mt_srand((double)microtime()*1000);
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, ad them to a list
while ($file = $imgs->read()) {
if (eregi("swf", $file))
$imglist .= "$file ";
} closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];
//display random swf
echo '<embed src="'.$img_folder.$image.'" quality="high"
pluginspage="https://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="100"
height="100"></embed>';
?>

Random SWF On Page Load Script Download

If you want to display random image on page load called from a specific folder then change file extension from swf to jpg or gif or png in above code and replace embed code with image code as following. Adjust image size according to your need.

Random Image Display Onload PHP Script

<?php
$imglist='';
//$img_folder is the variable that holds the path to the swf files or images files.
// see that you dont forget about the "/" at the end
$img_folder = "images/";
mt_srand((double)microtime()*1000);
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, ad them to a list
while ($file = $imgs->read()) {
if (eregi("jpg", $file))
$imglist .= "$file ";
} closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];
//display random swf
echo '<img src="'.$img_folder.$image.'" width="100"
height="100">';
?>

Related Posts

Previous post:

Next post: