PHP gets all files (including subdirectories) in the specified directory

Source: Internet
Author: User
PHP gets all files (including subdirectories) in the specified directory

Test environment under Linux, if you want to be in Windows, correct the way that directory paths are found in $new_dir

 php function Get_file_list ($dir) {$file _list=Array (); $file _dir_list=Array (); $dir _list= Scandir ($dir);//Find a directoryforeach($dir _list as$r) {        if($r = ='.'|| $r = ='..')          {            Continue; } $new _dir= $dir.'/'. $r; if(Is_dir ($new _dir)) {$file _dir=get_file_list ($new _dir); $file _dir_list=Array_merge ($file _dir_list, $file _dir); }        Else{$file _list[]=$new _dir; }    }         returnArray_merge ($file _list, $file _dir_list);} $file _list= Get_file_list ('.');p Rint_r ($file _list);

Scandir Definition and usage

The Scandir () function returns an array that contains the files and directories in the specified path.

If successful, returns an array, or false if it fails. If directory is not a directory, returns a Boolean value of false and generates an e_warning-level error.

Grammar

Scandir (Directory,sort,context)
Parameters Description
Directory Necessary. Specifies the directory to scan.
Sort Optional. Specify the order of arrangement. The default is 0 (ascending). If 1, it is descending.
Context Optional. The environment that specifies the directory handle. Context is a set of options that modify the behavior of a directory stream.

Example

 
  Phpprint_r (Scandir ("images"));

Output:

Array ([0] = = . [ 1] = =. [2] = = dog.jpg[3] = house.jpg[4] = logo.gif)

The above describes the PHP to get all the files in the specified directory (including subdirectories), included in the content, want to be interested in PHP tutorial friends helpful.

  • 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.