PHP traversal folder and file column representation example sharing

Source: Internet
Author: User
Tags php define

  This article mainly introduces the PHP traversal folder and file list examples, the need for friends can refer to the following

For PHP traversal directory and file list wrote a simple class, and attached to the use of examples, we refer to the use of the bar   code as follows: <?php define (' DS ', directory_separator);   Class getdirfile{     //return array     private $DirArray  = Array ();     Privat E $FileArray = Array ();     Private $DirFileArray = Array ();       Private $Handle, $Dir, $File;      //Get directory list     Public function Getdir (& $Dir) {        Is_dir ($ Dir)) {            if (false!= ($Handle = Opendir ($Dir)) {        &N Bsp       while (false!= ($File = Readdir ($Handle)) {                    if ($File!= '. ' && $File!= ' ... ' &&!strpos ($File, '. ')) {                        $DirArray [] = $File;     & nbsp              }         &nbsp      }                 Closedir ($Handle);                    }else{            $ dirarray[] = ' [Path]: '. $Dir. ' isn't a Dir or not found! ';        }         return $DirArray;    }      //Get file list     Public function GetFile (& $Dir) {      &N Bsp if (Is_dir ($Dir)) {            if (false!= ($Handle = Opendir ($Dir)) {    &NB Sp           while (false!= ($File = Readdir ($Handle)) {                    if ($File!= '. ' && $File!= ' ... ' && strpos ($File, '. ')) {                        $FileArray [] = $File;                    }                             &NBSP ;   Closedir ($Handle);                    }else{            $ filearray[] = ' [Path]: '. $Dir. ' isn't a Dir or not found! ';        }         return $FileArray;    }      //Get directory/File list     Public function Getdirfile (& $Dir) {    &NB Sp   if (Is_dir ($Dir)) {            $DirFileArray [' dirlist '] = $this->getdir ($Dir);             if ($DirFileArray) {                for Each ($DirFileArray [' dirlist '] as $Handle) {                    $File = $Dir. DS. $Handle;                     $DirFilearray[' filelist ' [$Handle] = $this->getfile ($File);                            }        }else{            $DirFileArray [] = ' [Path]: '. $Dir. ' isn't a Dir or not found! ' ;        }         return $DirFileArray;    }  }?>        instance: (relative path or absolute path)   1. Get directory list     code as follows: <?php $Dir _ Dir  = './example '; $getDirFile = new Getdirfile (); $getDir = $getDirFile->getdir ($Dir _dir); Print_r ($getDir);?>     display   code as follows: <?php $File _one_dir = './example/example_one '; $File _two_dir = ' e:/workspace/mycode/getdirfile/example/example_two ';   $getDirFile = new Getdirfile (); $getFile _one = $getDirFile->getfile ($File _one_dir); $getFile _two = $getDirFile->getfile ($File _two_dir);   Print_r ($getFile _one); Print_r ($getFile _two);?>      2. Get file list     code as follows: <?php $File _one_dir = './example/example_one '; $File _two_dir = ' e:/workspace/mycode/getdirfile/example/example_two ';   $getDirFile = new Getdirfile (); $getFile _one = $getDirFile->getfile ($File _one_dir); $getFile _two = $getDirFile->getfile ($File _two_dir);   Print_r ($getFile _one); Print_r ($getFile _two);?>       display code is as follows: Array (    [0] => example.sql     [1] = > Example.txt)   Array (    [0] => example.php)       3. Get directory/File list     code as follows : <?php $Dir _dir  = './example ';   $getDirFile = new Getdirfile (); $getDirFile  = $getDirFile->getdirfile ($Dir _dir);   Print_r ($getDirFile);?>       display   code as follows: Array (    [dirlist] => array         (            [0] => example_one             [1] => example_tWo        )       [filelist] => Array         (    & nbsp       [Example_one] => Array                 (    &NB Sp               [0] => example.sql               &N Bsp     [1] => example.txt                )               [example_two] => Array                 (    &NB Sp               [0] => example.php               &N Bsp )        )
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.