Php read directory displays instances in a list

Source: Internet
Author: User

Example 1

The code is as follows: Copy code

<? Php
 
$ GetUrl = (empty ($ _ GET ['URL'])? './': $ _ GET ['URL']. '/';
 
Function fileName ($ dir)
{
 
$ FileAll = scandir ($ dir, 0 );
$ PathDir = $ pathFile = array ();
$ Count = count ($ fileAll );
 
If ($ count <= 2 ){
Echo "empty directory <br/> ";
    }
     
Foreach ($ fileAll as $ pdf ){
If (is_dir ($ dir. $ pdf )){
$ PathDir [] = $ pdf;
} Else {
$ PathFile [] = $ pdf;
        }
    }
 
Foreach ($ pathDir as $ pd ){
If ($ pd = '.' or $ pd = '..') continue;
Echo "<a href = "? Url = $ dir $ pd "> $ pd </a> <br/> ";
    }
 
Foreach ($ pathFile as $ pf ){
Echo "<a href =" $ dir $ pf "target =" _ blank "> $ pf </a> <br/> ";
    }
 
If ($ dir! = './'){
$ Dir = rtrim ($ dir ,'/');
$ Dir = explode ('/', $ dir );
Unset ($ dir [sizeof ($ dir)-1]);
$ Dir = implode ('/', $ dir );
Echo "<a href = "? Url = $ dir "> Go Back </a> ";
    }
 
}
 
FileName ($ getUrl );

Example 2

The code is as follows: Copy code

<? Php

/**
* Goofy 2011-11-30
* GetDir () is used to go to the folder list, and getFile () is used to go to the file list under the corresponding folder. The difference between the two is to determine whether there are files with the "." suffix, and the others are the same.
*/

// Obtain the file directory list. This method returns an array.
Function getDir ($ dir ){
$ DirArray [] = NULL;
If (false! = ($ Handle = opendir ($ dir ))){
$ I = 0;
While (false! ==( $ File = readdir ($ handle ))){
// Remove "". "," .. ", and files with the suffix". xxx"
If ($ file! = "." & $ File! = ".."&&! Strpos ($ file ,".")){
$ DirArray [$ I] = $ file;
$ I ++;
   }
  }
// Close the handle
Closedir ($ handle );
 }
Return $ dirArray;
}

// Obtain the file list
Function getFile ($ dir ){
$ FileArray [] = NULL;
If (false! = ($ Handle = opendir ($ dir ))){
$ I = 0;
While (false! ==( $ File = readdir ($ handle ))){
// Remove "". "," .. ", and files with the suffix". xxx"
If ($ file! = "." & $ File! = ".." & Strpos ($ file ,".")){
$ FileArray [$ I] = "./imageroot/current/". $ file;
If ($ I == 100 ){
Break;
    }
$ I ++;
   }
  }
// Close the handle
Closedir ($ handle );
 }
Return $ fileArray;
}

// Call the getDir ("./dir ")...... It can be an absolute or relative path.
?>

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.