dongwuchu0314 2014-06-15 06:25
浏览 8

排除子目录php

I want to list all files at directory and exclude sub directory (newfiles)

i tried to use (if($file != "newfiles") ) but no thing change

foreach (ListFiles("/data") as $key=>$file){

if($file != "newfiles"){ 
$files=array_pop(explode('/',$file)); 
$lfile[]=$files;
}

}


function ListFiles($dir) { 

    if($dh = opendir($dir)) { 

        $files = Array(); 
        $inner_files = Array(); 

        while($file = readdir($dh)) { 
            if($file != "." && $file != ".." && $file[0] != '.') { 
                if(is_dir($dir . "/" . $file)) { 
                    $inner_files = ListFiles($dir . "/" . $file); 
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else { 
                    array_push($files, $dir . "/" . $file); 
                } 
            } 
        } 

        closedir($dh); 
        return $files; 
    } 
}  
  • 写回答

3条回答 默认 最新

  • douhui5529 2014-06-15 06:32
    关注

    You can use below function to exclude directory you want

      function glob_deep($pattern, $exclude = array(), $flags = 0)
       {
         $files = glob($pattern, $flags);
         foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
         {
           if(!in_array($dir, $exclude)) {
              $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
           }
         }
      return $files;
      }
    

    and call it like that

    $files =  glob_deep('data', array('newfiles'));
    var_dump($files);
    

    hope this helps

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比