PHP uses recursive traversal of file implementations within a specified folder

Source: Internet
Author: User
The following small series for everyone to bring a PHP recursive traversal in the specified folder file implementation method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

This morning on the subway saw a chapter on files and folders, just recently to understand the Linux file system, I think the access to file properties and Shell commands very much like, so want to practice at night.

found that the PHP folder function does not seem to provide the traversal of all the files under the folder (including subdirectories of files), so, you want to implement one yourself.

When I write, I find that some of the functions of the Action folder are not what you think. For example, DirName () Gets the path to the folder where the file is located, based on the full file path provided, but if you pass in a folder, it takes its parent folder. This should be understood. BaseName () is also the same reason, the Incoming folder path is the name of the folder. These two functions return no '/' at the end of the result.

<?php$dirpath= '/home/www/test ';//directory, end not add '/'//Perform traversal Recursion_readdir ($ Dirpath)/*** @summary repeat times characters char* @param $char need to repeat characters * @param $times repetitions * @return Returns a string consisting of repeating characters */function Forchar ($  Char= '-', $times =0) {$result = ';  for ($i =0; $i < $times; $i + +) {$result. = $char; } return $result;} /*** @summary Recursive Read directory * @param $dirPath directory * @param $Deep = 0 depth for indentation, no manual setting * @return no */function recursion_readdir ($dirPath, $ Deep=0) {$resDir =opendir ($dirPath), while ($basename =readdir ($resDir)) {//current file path $path = $dirPath. '   /'. $basename; if (Is_dir ($path) and $basename! = '. ' and $basename! = ' ... ') {//is the directory, print the directory name, continue to iterate echo forchar ('-', $Deep). $basename. '    /<br/> ';   $Deep ++;//Depth +1 recursion_readdir ($path, $Deep); }else if (basename ($path)! = '. ' and basename ($path)! = ' ... ') {//Not folder, print file name echo forchar ('-', $Deep). basename ($path). '   <br/> '; }} closedir ($resDir);} 
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.