Simple Example of randomly displaying images in php

Source: Internet
Author: User

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]");
?>

Related Article

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.