Php implements a summary of random display image methods ,. Php implements a summary of the random display image method. php generates a random number through the rand () function. This function can generate a number within a specified range. this code generates a random number, randomly select php to achieve a summary of the random display image method,
Php uses the rand () function to generate a random number. This function can generate a number within a specified range.
This code randomly selects an image through the random number generated
<?php srand( microtime() * 1000000 ); $num = rand( 1, 4 ); switch( $num ) { case 1: $image_file = "/home/images/alfa.jpg"; break; case 2: $image_file = "/home/images/ferrari.jpg"; break; case 3: $image_file = "/home/images/jaguar.jpg"; break; case 4: $image_file = "/home/images/porsche.jpg"; break; } echo "Random Image : ";?>
Method 2:
<? $ Handle = opendir ('./'); // the current directory while (false! ==( $ File = readdir ($ handle) {// traverse the list ($ filesname, $ kzm) of the directory where the php Tutorial file is located = explode (". ", $ file); // Obtain the extension if ($ kzm =" gif "or $ kzm =" jpg ") {// filter files if (! Is_dir ('. /'. $ file) {// folder filter $ array [] = $ file; // Save the qualified file name to an array }}$ suiji = array_rand ($ array ); // use the array_rand function to randomly extract a unit from the array?> ">
Method 3:
<? Php /************************************** * ******* Filename: img. php * Author: freemouse * Usage: **************************************** * *******/if ($ _ GET ['Folder']) {$ folder = $ _ GET ['Folder'];} else {$ folder = '/images /';} // location where the image file is stored $ path =$ _ SERVER ['document _ root']. "/". $ folder; $ files = array (); if ($ handle = opendir ("$ path") {while (false! ==( $ File = readdir ($ handle) {if ($ file! = "." & $ File! = ".. ") {If (substr ($ file,-3) = 'GIF' | substr ($ file,-3) = 'jpg ') $ files [count ($ files)] = $ file ;}} closedir ($ handle); $ random = rand (0, count ($ files)-1 ); if (substr ($ files [$ random],-3) = 'GIF') header ("Content-type: image/gif "); elseif (substr ($ files [$ random],-3) = 'jpg ') header ("Content-type: image/jpeg "); readfile ("$ path/$ files [$ random]");?>
The above is all the content of this article. I hope you will like it.
Random, php generates a random number through the rand () function. This function can generate a number in a specified range. the random number generated by this code is randomly selected...