Using PHP to traverse the function code of folders and subdirectories _php tutorial

Source: Internet
Author: User
Tags glob
The function we want to use is Scandir, which is to list the files and directories in the specified path, just like Dir.


> A more powerful Glob () function that returns a file name or directory that matches the specified pattern in the form of an array.
> Friendship remind, do not like small evil in front of the computer for too long, otherwise it will be like a small evil to hell of high blood sugar.

I. Traverse a single-layer folder:

> The problem with scanning single-layer folders is that the results of the two functions are different, but the performance is not quite the same.
The > Scandir function provides an additional two lines, namely "." and "..", while Glob is not.
Copy CodeThe code is as follows:
function Get_dir_scandir () {
$tree = Array ();
foreach (Scandir ('./') as $single) {
echo $single. "
\ r \ n ";
}
}
Get_dir_scandir ();

function Get_dir_glob () {
$tree = Array ();
foreach (Glob ('./* ') as $single) {
echo $single. "
\ r \ n ";
}
}
Get_dir_glob ();

Two. Recursively traverse the file tree:

> 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 and. /a.php, the results will appear on the scan report two times, it is very strange.
Copy CodeThe code is as follows:
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));

> Glob function scans are often accurate and automatically sorted alphabetically, seemingly the best solution.
Copy CodeThe code is as follows:
$path = ' ... ';
function Get_filetree ($path) {
$tree = Array ();
foreach (Glob ($path. ' /* ') as $single) {
if (Is_dir ($single)) {
$tree = Array_merge ($tree, Get_filetree ($single));
}
else{
$tree [] = $single;
}
}
return $tree;
}
Print_r (Get_filetree ($path));

http://www.bkjia.com/PHPjc/324411.html www.bkjia.com true http://www.bkjia.com/PHPjc/324411.html techarticle The function we want to use is Scandir, which is to list the files and directories in the specified path, just like Dir. With the more powerful Glob () function, the function is returned in the form of an array with ...

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