PHP implementation of random display picture method summary

Source: Internet
Author: User
Tags php tutorial rand

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.

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.