Introduce several methods of PHP traversal directory, you can traverse directory and directory files, for everyone to reference
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.
This function can list all the files in the specified directory (including sub-directories)
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, 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.
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, 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
The code is as follows:
Some of the common examples
The code is as follows:
<?php$dir= "E:/video"; Enter a different path here//php traverse the folder under All Files $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 through all the files, help me to save all the file names as an array.
The code is as follows:
<?php$s=explode ("/n", trim (' dir/b e://video '));p rint_r ($s);? ><?php $dir = "E:/video"; Enter a different path here//php traverse all files under the folder $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 of closedir ($handle);? ><?php $dir = "."; Enter a different path here//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 file name}} Closedir ($handle);p rint_r ("<pre>");p Rint_r ($array _file);p rint_r ("</pre>");