PHP recursive traversal of File Tree Code _php Tutorial

Source: Internet
Author: User
Tags glob
In the recursive Scan folder tree problem, or Glob function of the performance of a bit better, very accurate said. The > Scandir function will be inexplicably scanned two times. File, that is, if there are two files for the minor evil.


>.. /b.php Tutorials and: /a.php, the results will appear on the scan report two times, it is very strange.

The code is as follows Copy Code

Update at 2010.07.25-the following code is void
$path = ' ... ';
function Get_filetree_scandir ($path) {
$tree = Array ();
foreach (Scandir ($path) as $single) {
if (Is_dir ('.. /'. $single)) {
$tree = Array_merge ($tree, Get_filetree ($single));
}
else{
$tree [] = '. /'. $single;
}
}
return $tree;
}
Print_r (Get_filetree_scandir ($path));

Update at 2010.07.25-The following is the new code
$path = './';
function Get_filetree_scandir ($path) {
$result = Array ();
$temp = Array ();
if (!is_dir ($path) | |! Is_readable ($path)) return null; Detecting Catalog Validity
$allfiles = Scandir ($path); Get all files and folders under directory
foreach ($allfiles as $filename) {//Walk through files and folders in directory
if (In_array ($filename, Array ('. ', '. '))) continue; Ignore. And..
$fullname = $path. ' /'. $filename; Get the full file path
if (Is_dir ($fullname)) {//Is the directory Word continues recursion
$result [$filename] = Get_filetree_scandir ($fullname); Recursive start
}
else {
$temp [] = $filename; If it is a file, it is stored in an array
}
}
foreach ($temp as $tmp) {//the contents of the temporary array are stored in an array of saved results
$result [] = $tmp; This allows the folder to be in front of the file in the back
}
return $result;
}
Print_r (Get_filetree_scandir ($path));

http://www.bkjia.com/PHPjc/631703.html www.bkjia.com true http://www.bkjia.com/PHPjc/631703.html techarticle in the Recursive Scan folder tree problem, or Glob function of the performance of a bit better, very accurate said. The > Scandir function will be inexplicably scanned two times. /file, that is, if small ...

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