Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to display random image from a directory?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 171
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: