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)
Copy Code code 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.
Copy Code code 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
Copy Code code 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
Copy Code code 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.
Copy Code code as follows:
<?php
$s =explode ("n", Trim (' dir/b e://video '));
Print_r ($s);
?>
<?php
$dir = "E:/video";//Enter a different path
//php Traverse folder All Files
$handle =opendir ($dir. ");
echo "File:<br>";
while (false!== ($file = Readdir ($handle)))
{
if ($file!= "." && $file!= "...") {
$file = $file. ', ';//output filename
$file =explode (', ', $file);
}
}
Print_r ($file);//output is an array
Closedir ($handle);
?>
<?php
$dir = "."; Here, enter the other path
//php traverse 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 filename
}
}
Closedir ($handle);
Print_r ("<pre>");
Print_r ($array _file);
Print_r ("</pre>");
?>