Php implementation of random display image method summary, _ PHP Tutorial

Source: Internet
Author: User
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...

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.