PHP Snippets
Name:Random Image
Description: Display random image from specific directory.
Code below...
>
<?php
//Make sure you ONLY have image files on the path that you specified below
//Function to display all files from the folder
//View the function here: http://www.r2xdesign.net/viewcontent-25
function viewfiles($path){
$ff = array();
if ($dir = @opendir("./$path")){
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$ff[] = $file;
}
}
closedir($dir);
}
$result = implode("*",$ff);
return $result;
}
//Change this path to the actual path of your images
$dir_path = "imgs";
$dir = viewfiles($dir_path);
$exp = explode("*",$dir);
//Randomize images
srand((float) microtime() * 10000000);
$pic = $exp[array_rand($exp)];
//This display the random image
echo "<a href=\"$dir_path/$pic\"><img src=\"$dir_path/$pic\" border=\"0\" alt=\"\"/></a>"
?>
//Make sure you ONLY have image files on the path that you specified below
//Function to display all files from the folder
//View the function here: http://www.r2xdesign.net/viewcontent-25
function viewfiles($path){
$ff = array();
if ($dir = @opendir("./$path")){
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$ff[] = $file;
}
}
closedir($dir);
}
$result = implode("*",$ff);
return $result;
}
//Change this path to the actual path of your images
$dir_path = "imgs";
$dir = viewfiles($dir_path);
$exp = explode("*",$dir);
//Randomize images
srand((float) microtime() * 10000000);
$pic = $exp[array_rand($exp)];
//This display the random image
echo "<a href=\"$dir_path/$pic\"><img src=\"$dir_path/$pic\" border=\"0\" alt=\"\"/></a>"
?>