Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Random image display in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 126
    Comment on it

    You will face any situation in php where you want to display all the images in a random manner. Below is the code which will display all the images in random manner with the help of srand() function which seeds the random generator. It store all your images that user want to display in the same folder you specified. It script reads the entire folder you specify and displays the contents at random. You can also add a width also to last line.

    $Image_Path = "/path/to/site/root"; // Root Path - no slash on end 
    $Image_Folder = "Images"; // Directory name of the images folder 
    srand((double) microtime() * 10000000); 
    $imgdirpath = opendir("$Image_Path/$Image_Folder"); 
    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"; 
    

 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: