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="http://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">';
?>
  • Share/Bookmark

, , , ,

16 Reviews

  1. joe says:

    this is the best solution of any that I have seen.

    Thank you!!

  2. Kevin says:

    Thanks … this has been a life saver for me. I do have a question though. Once a random swf is playing, can it be set to play the other files in the directory in an endless loop until the page is reloaded?

  3. King says:

    @Kevin: Of course. Just try that. Echo that whatever you want in loop. Any file.

  4. Kevin says:

    I should have mentioned that I’m just learning … how do I do that? Thanks again!

  5. King says:

    @Kevin: Okay you have to just change swf in following line
    if (eregi(“swf”, $file))
    which becomes
    if (eregi(“jpg”, $file))
    in case of jpg images
    and change the lines in echo for images.
    echo > img src tags instead of embed tags
    See the second example I posted above.

  6. Kevin says:

    Great … thanks again!

  7. me the script works correctly, but what if I want to put a swf with transparent background? Where do we draw the parameters?

  8. sid says:

    hi nice script..used it..and working fine. i have a question, though. how can we make it go to next swf or previous swf files?

  9. Sheetal says:

    Hi
    Thanks for this wonderful information it help me alot..

  10. King says:

    @sid: Just refresh the page. Its random! Remember?

  11. King says:

    @Francisco Aldunate: There is a wmode parameter for flash files to make these transparent.
    See here: http://www.techmynd.com/how-to.....ansparent/

  12. Betsy says:

    I have been searching forever for a script like this. Thanks!!! I do have one question though. I am only working with 3 swf files right now and it pulls up the same one over and over quite often. Is there a way to change this code to make it pull them in order instead of randomly? I am very, very new to PHP so please be specific. Thanks for you help.

  13. Jim says:

    WOW!… amazing… this has been a tough one to find out there.

    Is there a way that I can supress the #55555 random number at the end of the url?

  14. Travis says:

    Awesome code thanks! Is there a way to have it pull random files from a folder that contains gifs and swf files. For instance, on one load it might be a swf but on another load it may be an animated gif.

  15. flashcode says:

    Is it possible to allow full screen in the PHP code?

Leave a Review

Incoming Searches

php onload, how do you get a page to display a random swf file, random flash php, flash swf script, php code play movies, php script upload swf, php show swf same page, php swf, php swf edit, php swf embed, php swf play, play swf onload, php tag cloud script explode, play 2 swf files in php, play random flash movies on a page, play random swf from a folder in flash, Php script image dynamic when onload, php script embed swf, php onload example, php onload post, php open sfw, php play swf file, php random code loop, php random file if swf, php random flash, php random flash images, php random image and flash, php random include swf, php random jpg, php random swf, php read directory script, php rotate swf directory, put a swf in php, random data swf free, random embed script swf, script to list array of movies in a directory, scripting php dan swf, scripting to make a book in flash, showing a random swf , swf code with php, swf insert php script, swf onload, swf php code, swf random image reload, swf random images, swf rotating image, to create flash images PHP Script, uploading and displaying swf file using php and mysql, script swf on refresh page, script swf for php, random flash movie on load, RANDOM FLASH SCRIPT, random flash ด้วย php, random jpg avatar script, Random Number Generator file fhash, random number swf embed php, random page php, random picture load flash script, random swf, random swf on load, random swf on refresh php, random swf php, script php para criar swf, using PHP to dynamically load SWF, 2010 load random flash movies, flash load random swf, Flash php contact script, flash script height 100%, for loop through random swf, free php code blog comments, free php play flash swf, generate loop swf with php, have to reload swf in wordpress, how embed flash in a php mod rewrite page, how to add onload flash effect in php, how to add swf to page onload, how to edit script in swf, how to embed a swf in a php script, flash load movies and swf scripts, Flash fime per Random php, Booking Script - Flash PHP & MySQL with Admin, check php script requested by swf, display flash embedded php script, display random flash movie php, display swf in loop, display swf in php, Dynamic Image Display Based on PHP Variable, dynamically load swf files in embedded swf, embed flash swf script php, embed random flash php, eregi on code igniter, eregi swf, flash book php script, How to pull a random flash file, how to put random jpg in flash, how to put swf on website in php script, onload flash, onload function php flash script, onload image php, onload php function, onload PHP images, onload swf, page counter flash embedded code, php code swf, php elseif load swf, php function random load, php how to random questions, php load random flash file, php load swf random, onLoad as2, onload a include php, htaccess random page load, http://www phpmagicbook com/random-flash-swf-onload-php-script/, image random flash, insert swf php code, Is there a way to embed swf in email in php, javascript random SWF, JPG to replace SWF, load php image variable to flash, load swf in php, Loading a Random swf in php, loop code swf files, object random rotating script flash, on page load new swf, php net onload