Several ways to traverse directories and folders in PHP

Source: Internet
Author: User
Tags foreach explode glob learn php zip

Traverse the directory or traverse the directory of the specified type of file, this is every child shoe in the writing program will inevitably use. PHP itself also provides a lot of gray useful functions, use them correctly, there is no mistake.

The following is a summary of my personal learning process, I hope to learn PHP's children's shoes help.

This function can list all the files in the specified directory (including the subdirectories)

The 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. ' <br/> ';
}
}
//Simple demo, listing all files in current directory

GetFiles (__dir__);

Scandir () is an array of all the files and directories in the specified directory, and in PHP, a glob (), Glob () has 2 parameters, and the 2nd argument is optional, later. Look directly, using Glob () How to traverse the directory.

//
As you can see, glob () has filtered out '. ' In the content returned. and '.. ', where * means traversing all the files in the directory. Accordingly, if changed to *.txt, will traverse the directory under the TXT file. Is it convenient? The convenience of it is more than that, according to Yuan Fang said, there is still a big secret inside, what is it? Later, if you are interested, you can give me a message exchange.

The code is as follows

function GetFiles ($path) {
foreach (Glob ($path) as $afile) {
if (Is_dir ($afile))
{GetFiles ($afile. ') /*'); else {echo $afile. ' <br/> '; }
}
//Simple demo, listing all files in current directory
GetFiles (__dir__); 0

Since the use of *.txt, will traverse the directory under the TXT file, if I want it to traverse a number of formats at the same time file? What to do? There must be children's shoes. Think of the array, and then quickly write to replace it in {*.txt,*.jpg,*.zip,...}, and of course quickly found that the program returned false, nothing. Don't be disappointed, this involves the 2nd optional argument that you just mentioned, this parameter is used to change the behavior of the glob, what are the specific, you can consult the PHP manual, here is not much, just say a glob_brace, this is used to expand {a,b,c, ...} to match ' A ', ' B ' or ' C ', ... Of The 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

The code is as follows


<body>
<?php
function traverse ($path = '. ') {
$current _dir = Opendir ($path); Opendir () returns a directory handle that fails to return false
while (($file = Readdir ($current _dir))!== false) {//readdir () returns an entry in the Open directory handle
$sub _dir = $path. Directory_separator. $file; Building a subdirectory path
if ($file = = '. ' | | $file = = ' ... ') {
Continue
else if (Is_dir ($sub _dir)) {//If it is a directory, make a recursive
Echo ' Directory '. $file. ':<br> ';
Traverse ($sub _dir);
else {//If it is a file, direct output
Echo ' File in Directory '. $path. ': ' . $file. ' <br> ';
}
}
}

Traverse (' xxtt ');
?>
</body>

Some common examples

The code is as follows

<?php
$dir = "E:/video"; Enter another path here
PHP traverses all files under the folder
$handle =opendir ($dir. ");
echo "File:<br>";
while (false!== ($file = Readdir ($handle)))
{
if ($file!= "." && $file!= "...") {
Echo $file; Output file name
}
}
Closedir ($handle);
?>

Using this code to traverse all the files, save all the file names as an array.

The code is as follows

<?php
$s =explode ("n", Trim (' dir/b e://video '));
Print_r ($s);
?>

<?php
$dir = "E:/video"; Enter another path here
PHP traverses all files under the folder
$handle =opendir ($dir. ");
echo "File:<br>";
while (false!== ($file = Readdir ($handle)))
{
if ($file!= "." && $file!= "...") {
$file = $file. ', '; Output file name
$file =explode (', ', $file);
}
}
Print_r ($file);//output is an array.
Closedir ($handle);
?>

<?php
$dir = "."; Enter another path here
PHP traverses all files under the folder
$handle =opendir ($dir. ");
echo "File:<br>";
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 ("<pre>");
Print_r ($array _file);
Print_r ("</pre>");
?>

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.