PHP Traversal directories and folders in a variety of ways detailed _php instances

Source: Internet
Author: User
Tags glob
Traverse a directory or traverse a directory of files of a specified type, which is unavoidable for every child's shoe when writing a program. PHP itself also provides a number of functions that are often useful, using them correctly, without any errors.
Here is a summary of my personal learning process, I hope to learn PHP children's shoes to help.
This function can list all the files in the specified directory (including sub-directories)
Copy CodeThe code is as follows:
function GetFiles ($path) {
foreach (Scandir ($path) as $afile)
{
if ($afile = = '. ' | | $afile = = ' ... ') Continue
if (Is_dir ($path. ' /'. $afile))
{
GetFiles ($path. ' /'. $afile);
} else {
echo $path. ' /'. $afile. '
';
}
}
}//Simple demo, list all the files in the current directory
GetFiles (__dir__);

Scandir () is an array that returns all the files and directories in the specified directory, and in PHP, it also provides a very powerful function glob (), Glob () has 2 parameters, and the 2nd parameter is optional and later. In direct view, how to traverse the directory with Glob ().
As you can see, glob () has filtered out '. ' In the content returned. and '. ', where * means traversing all files in the directory. Accordingly, if *.txt is changed, the TXT file under the directory will be traversed. Isn't it convenient? Its convenience is more than this, according to Yuan Fang said, there is a big secret, what is it? Later, if you are interested, you can give me a message to exchange.
Copy CodeThe code is as follows:
function GetFiles ($path) {
foreach (Glob ($path) as $afile) {
if (Is_dir ($afile))
{GetFiles ($afile. ') /*'); } else {echo $afile. '
'; }
}
}//Simple demo, list all the files in the current directory
GetFiles (__dir__); 0

Now that you're using *.txt, you'll traverse the TXT file in the directory, so what if I want it to traverse a file of several formats at the same time? What to do? There must be children's shoes think of the array, and then quickly write out to replace {*.txt,*.jpg,*.zip,...}, of course, also quickly found that the program returned false, nothing. Do not be disappointed, this relates to the 2nd optional parameter that is mentioned just now, this parameter is used to change the behavior of Glob, specific what, can consult PHP manual, here not to speak, only say a glob_brace, this is used to expand {a,b,c, ...} to match ' A ', ' B ' or ' C ', ... Of Usage is as follows: foreach (Glob ($path. ') /{*.txt,*.jpg,*.zip,...} ', Glob_brace) as $fileName) {...}
As for the complete traversal directory of all the specified file type functions, we can look at the following example

Traverse folders and subfolders all files
Copy CodeThe code is as follows:


function traverse ($path = '. ') {
$current _dir = Opendir ($path); Opendir () returns a directory handle, failure returns false
while (($file = Readdir ($current _dir))!== false) {//readdir () returns an entry in the Open directory handle
$sub _dir = $path. Directory_separator. $file; To build a subdirectory path
if ($file = = '. ' | | $file = = ' ... ') {
Continue
} else if (Is_dir ($sub _dir)) {//If it is a directory, recursively
Echo ' Directory '. $file. ':
';
Traverse ($sub _dir);
} else {//If it is a file, direct output
Echo ' File in Directory '. $path. ': ' . $file. '
';
}
}
}

Traverse (' xxtt ');
?>



Some of the common examples
Copy CodeThe code is as follows:
$dir = "E:/video"; Enter a different path here
PHP iterates through all the files in the folder
$handle =opendir ($dir. ".");
echo "File:
";
while (false!== ($file = Readdir ($handle)))
{
if ($file! = "." && $file! = "...") {
Echo $file; Output file name
}
}
Closedir ($handle);
?>

Using this code to traverse through all the files, help me to save all the file names as an array.
Copy CodeThe code is as follows:
$s =explode ("/n", trim (' dir/b e://video '));
Print_r ($s);
?>
$dir = "E:/video"; Enter a different path here
PHP iterates through all the files in the folder
$handle =opendir ($dir. ".");
echo "File:
";
while (false!== ($file = Readdir ($handle)))
{
if ($file! = "." && $file! = "...") {
$file = $file. ', '; Output file name
$file =explode (', ', $file);
}
}
Print_r ($file);//The output is an array.
Closedir ($handle);
?>
$dir = "."; Enter a different path here
PHP iterates through all the files in the folder
$handle =opendir ($dir. ".");
echo "File:
";
Defines an array for storing file names
$array _file = Array ();
while (false!== ($file = Readdir ($handle)))
{
if ($file! = "." && $file! = "...") {
$array _file[] = $file; Output file name
}
}
Closedir ($handle);
Print_r ("

");
Print_r ($array _file);
Print_r ("
");
?>
  • 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.