Why can't this built-in function be written like this?

Source: Internet
Author: User
Tags glob
Why can't I write this built-in function like this? PHPcode $ path = 'E:/Zl/materials-1/database/3005 Audi A6L new C6 factory materials '; functionget_filetree ($ path) {$ tree = array, why can't I write this built-in function 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 ));
}

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.