PHP Traversal directory/file in two ways

Source: Internet
Author: User

1. Use $obj = Dir ($dir) to return the directory object $obj, and then use its $obj->read () method to traverse;

1<?PHP2 /**3 * Created by Phpstorm.4 * User:zhang He5 * DATE:2015/6/86 * time:12:217  */8 functionTraversedir ($filedir ) {9     //Open DirectoryTen     $dir= @dir($filedir); One     //list files in a directory A      while(($file=$dir->read ())!==false){ -         if(Is_dir($filedir." /".$file) and ($file!=".") and ($file!="..")){ -             //recursive traversal of subdirectories theTraversedir ($filedir." /".$file); -}Else { -             //output file full path -             Echo"FileName:".$filedir." /".$file. "<br/>"; +         } -     } +     $dir-close (); A } at //testing: Traversing the current directory -Traversedir ("."); -?>

2, First Use $obj = Opendir ($dir) to get the object $obj, and then use Readdir ($obj) to traverse the directory

1<?PHP2 /**3 * Created by Phpstorm.4 * User:zhang He5 * DATE:2015/6/86 * time:12:217  */8 functionTraversedir ($dir){9     if(Is_dir($dir)){Ten         if($obj=Opendir($dir)){ One              while(($file=Readdir($obj)) !==false){ A                 if((Is_dir($dir." /".$file)) and$file! = "." and$file!=".."){ -                     Echo"<b><font color= ' red ' > FileName:</font></b>",$file, "<br>; -Traversedir ($dir." /".$file." /"); the}Else{ -                     if($file! = "." and$file!=".."){ -                         Echo $file." <br> "; -                     } +                 } -             } +             Closedir($obj); A         } at     } - } - //testing: Traversing the current directory -Traversedir ("."); -?>

PHP Traversal directory/file in two ways

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.