The main content of this section:
This section describes a php function for randomly displaying images. It is mostly used for displaying blog windows and photos at random.
Example:
Copy codeThe Code is as follows:
<? Php
/**
* Function: displays images randomly.
* Filename: img. php
* Usage:
*
*
**/
If ($ _ GET ['folder']) {
$ Folder = $ _ GET ['folder'];
} Else {
$ Folder = '/images /';
}
// The 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]");
?>