Home >Backend Development >PHP Tutorial > 活见鬼,为什么这个内置函数这样写不行

活见鬼,为什么这个内置函数这样写不行

WBOY
WBOYOriginal
2016-06-13 13:12:28931browse

见鬼,为什么这个内置函数这样写不行?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->$path = 'E:/Zl/资料-1/资料库/3005  奥迪A6L新型C6原厂资料';
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));


把{*.pdf,*.doc,*.ppt} 换成 * 就可以显示所有文件了,目录有些文件....

------解决方案--------------------
这样写
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;
}
<br><font color="#e78608">------解决方案--------------------</font><br>
你#6的写法也是可以的,但用错了目标<br>if(glob($path."*/*.{pdf,doc,ppt}", GLOB_BRACE)){<br>应为<br>if(glob($single."{pdf,doc,ppt}", GLOB_BRACE)){<br>目的是判断 $single 的后缀是否符合要求<br><br>我#4的代码:<br> //取得所有符合条件的文件名<br> foreach(glob($path."/*.{pdf,doc,ppt}", GLOB_BRACE) as $single) {<br>   if(is_file($single)) $tree[] = $single;<br> }<br> //遍历所有子目录<br> foreach(glob($path."/*", GLOB_ONLYDIR ) as $single) {<br>   $tree = array_merge($tree,get_filetree($single));<br> }<br> <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn