This article shares a PHP implementation of the random display of the picture function, you can specify the folder stored in the picture randomly displayed. Interested friends to study under it.
PHP generates random numbers through the rand () function, which produces a specified range of digits
This code randomly chooses a picture by generating random numbers
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 |
|
Method Two:
?
1 2 3 4 5 6 7 8 9 10 11 12-13 |
? $handle = Opendir ('./'); Current directory while (False!== ($file = Readdir ($handle)) {//Traverse the directory list ($filesname, $kzm) of the PHP tutorial file =explode (".", $file);//Get extension if ($kzm = = "gif" or $kzm = = "jpg") {//File filter if (!is_dir ('./'. $file)) {//folder filter $array []= $file; To save eligible file names to array}}} $suiji =ar Ray_rand ($array); Use the Array_rand function to randomly extract a cell from an array?> |
Method Three:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
<?php/********************************************** * Filename:img.php * author:freemouse * Usage: * * * * /if ($_get[' folder ']) {$folder =$_get[' folder '];} else{$folder = '/images/';}//Location of picture file $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 the entire contents of this article, I hope you can enjoy.