I'm new to PHP and this seems like I'm just asking for code but in reality I want to know how to implement a link along with the random selector.
My 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 = "../files/flash/";
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 (preg_match("/\.swf$/i", $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="650"
height="450"></embed>';
?>
This basically grabs a random flash file from my site's directory. Since it won't affect the URL bar, they won't be able to send something they like to someone (which is an unintentional security feature). I want them to be able to send a link or possibly direct click to download.
Also, I was thinking of as well as the random generator, to make a left and right arrow that they can scroll through all the .swf's in the chosen directory, if possible.
the site is www.nsgaming.us and it's the 'Random' button under the text logo.
What you are asking for is reading parameters from the URL so people can link their friends to this SWF embed, you can do that by using $_GET
Look at this example I coded.
$swfs = array();
$swf_location = '../files/flash/';
if($handle = opendir($swf_location)) {
while(false !== ($entry = readdir($handle))) {
if(strtolower(pathinfo($path, PATHINFO_EXTENSION)) == 'swf') {
$swfs[] = $entry;
}
}
closedir($handle);
}
if(isset($_GET['swf']) && in_array($_GET['swf'], $swfs)) {
$swf = $_GET['swf'];
} else {
$swf = $swfs[rand(0, count($swfs))];
}
echo '<embed src="' . $swf_location . $swf . '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="650" height="450"></embed><br />Send this to a friend: <input type="text" value="http://yourwebsite.com/thisfilesname.php?swf=' . $swf . '" length="55">';
You're previous script was very messy and did a lot of useless stuff, this is secure and faster