To display images from a directory randomly first of all we will give the path to the directory. Then we will select the directory. After that we will open the directory and will get all the files of the directory.
<?php
$Image_Path = ""; // Root Path
$Image_Folder = "images"; // Directory name of the folder storing your images
srand((double) microtime() * 10000000);
$imgdirpath = opendir("$Image_Path"."$Image_Folder"); //open the directory
while (false !== ($imgfile = readdir($imgdirpath))) {
if ($imgfile != "." && $imgfile != "..") {
$imgpath = "$Image_Path/$Image_Folder/$imgfile";
$imageimg[$imgfile] = basename($imgpath);
}
}
closedir($imgdirpath);
shuffle($imageimg);
list( ,$img_value) =each($imageimg);
echo "<img src=\"$Image_Folder/$img_value\">\n";
?>
When you will run this script you will see the random images everytime when you refresh the page.
0 Comment(s)