Why can't I write this built-in function like this? PHPcode $ pathE: Zl data-1 database 3005 Audi A6L new C6 factory data; functionget_filetree ($ path) {$ treearray (); foreach (glob ($ path. & quot ;{*. pdf ,*. doc ,*. ppt} & quot;, G. Why can't this built-in function be written like this?
PHP code
$ Path = 'E:/Zl/materials-1/database/3005 Audi A6L new C6 factory materials '; function get_filetree ($ path) {$ tree = array (); foreach (glob ($ path. "/{*. pdf ,*. doc ,*. ppt} ", GLOB_BRACE) as $ single) {if (is_dir ($ single) {$ tree = array_merge ($ tree, get_filetree ($ single ));} else {$ tree [] = $ single;} return $ tree;} print_r (get_filetree ($ path ));
Replace {*. pdf, *. doc, *. ppt} with * to display all files. some files in the directory ....
------ Solution --------------------
Write in this way
PHP code
Function get_filetree ($ path) {$ tree = array (); foreach (glob ($ path. "/*. {pdf, doc, ppt} ", GLOB_BRACE) as $ single) {if (is_file ($ single) $ tree [] = $ single;} foreach (glob ($ path. "/*", GLOB_ONLYDIR) as $ single) {$ tree = array_merge ($ tree, get_filetree ($ single);} return $ tree ;}
------ Solution --------------------
You can write your #6 statement, but you have used the wrong target.
If (glob ($ path. "*/*. {pdf, doc, ppt}", GLOB_BRACE )){
Should be
If (glob ($ single. "{pdf, doc, ppt}", GLOB_BRACE )){
The purpose is to determine whether the suffix of $ single meets the requirements.
My #4 code:
// Obtain all qualified file names
Foreach (glob ($ path. "/*. {pdf, doc, ppt}", GLOB_BRACE) as $ single ){
If (is_file ($ single) $ tree [] = $ single;
}
// Traverse all subdirectories
Foreach (glob ($ path. "/*", GLOB_ONLYDIR) as $ single ){
$ Tree = array_merge ($ tree, get_filetree ($ single ));
}