Php multiple methods for randomly displaying images _ PHP Tutorial

Source: Internet
Author: User
Tags random seed
Php supports multiple methods for randomly displaying images .? $ Handleopendir (.); current directory while (false! ($ Filereaddir ($ handle) {traverse the list ($ filesname, $ kzm) explode (., $ file) directory where the php Tutorial file is located; get the extension $ Handle = opendir ('./'); // Current Directory
While (false! ==( $ File = readdir ($ handle) {// traverse the directory where the php Tutorial file is located
List ($ filesname, $ kzm) = explode (".", $ file); // get the extension
If ($ kzm = "gif" or $ kzm = "jpg") {// file filtering
If (! Is_dir ('./'. $ file) {// folder filtering
$ 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.
?>
">

Instance 2

/*************************************** *******
* Filename: img. php
* Author: freemouse
* Web: www.zhutiai.com * mail: freemouse1981@gmail.com
* Date: 2010/12/27
* 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]");
?>


Readrand. php (this program actually generates a special webpage effect language)
$ Arrayall = file ("tp.txt" when reading the content of tp.txt to the array
$ Arrays = count ($ arrayall );
If ($ arrays = 1) {// because rand (0, 0) is wrong
$ Selectrand = 0;
} Else {
Srand (double) microtime () * 1000000); // sets the random seed.
$ Selectrand = rand (0, $ arrays-1 );
}
$ Exstr = explode (chr (9), $ arrayall [$ selectrand]); // randomly retrieve and divide all
?>
Document. write ('"target =" new ">" width = "200" height = "50" alt =" "> ');


HTML file


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.