PHP implementation of random display image method summary,
PHP generates a random number through the rand () function, which produces a specified range of numbers
This code randomly selects a picture by the resulting random number
<?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 Two:
< $handle = Opendir ('./'); The current directory while (False!== ($file = Readdir ($handle))) {//iterates through the PHP tutorial file directory list ($filesname, $KZM) =explode (".", $file);// Gets the extension if ($kzm = = "gif" or $kzm = = "jpg") {//File filter if (!is_dir ('./'. $file)) {//folder filter $array[]= $file;//Save the eligible file name in the array}}} $suiji =array_rand ($array); Use the Array_rand function to randomly extract a cell from an array?> ">
Method Three:
<?php/*********************************************** filename:img.php* author:freemouse* Usage:* * ************ /if ($_get[' folder ') {$folder =$_get[' folder '];} else{$folder = '/images/';} Location of the picture file $path = $_server[' Document_root ']. " /". $folder; $files =array (), if ($handle =opendir (" $path ")) {while (false!== ($file = Readdir ($handle))) {if ($file! =)." &A mp;& $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 mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1003994.html www.bkjia.com true http://www.bkjia.com/PHPjc/1003994.html techarticle PHP Implementation of random display image method Summary, PHP through the rand () function to generate a random number, this function can produce a specified range of numbers this code through the generated random number, randomly selected ...