PHP traverses the folder to read the images in the folder and displays the images by page.

Source: Internet
Author: User

Introduction: My website IMG has images such as 1.jpg, JPG ,...... 、N.jpg1_1.gif1_2.gif1_3.gif ,...... 、N.gif: Create an index. php file under the images directory to display all images in the images directory by page. The following are my methods. I wonder if there is any better way. Haha... Create an index. php file under images in the image folder. The content is as follows:

<? PHP
Echo "<HTML> $ Page = $ _ Get ['page']; // obtain the current page number.
$ Max = 3; // you can specify the maximum number of images displayed on each page.
$ Handle = opendir ('./'); // Current Directory
While (false! ==( $ File = readdir ($ handle) {// traverse the directory where the PHP file is located
List ($ filesname, $ kzm) = explode (".", $ file); // get the extension
If ($ kzm = "GIF" or $ kzm = "jpg" or $ kzm = "jpg") {// file filtering
If (! Is_dir ('./'. $ file) {// folder Filtering
$ Array [] = $ file; // Save the qualified file name to an array
$ I ++; // record the total number of images
}
}
}
For ($ J = $ Max * $ page; $ j <($ Max * $ page + $ max) & $ j <$ I; ++ $ J) {// control the number of images displayed by cyclic conditions
Echo " <br>"; // output image Array
}
$ Previus_page = $ page-1;
$ Next_page = $ page + 1;
If ($ previus_page <0 ){
Echo "Previous Page ";
Echo "<a href =? Page = $ next_page> next page </a> ";
}
Else if ($ page <= $ I/$ max ){
Echo "<a href =? Page = $ previus_page> previous page </a> ";
Echo "<a href =? Page = $ next_page> next page </a> ";}
Else {
Echo "<a href =? Page = $ previus_page> previous page </a> ";
Echo "next page ";
}
Echo "</center> </body> ?>

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.