0

我有一些error_log.txt文件,在中创建/var/www/html/log/'.date(Ymd).'/error_log.txt,文件夹表是这样的。

-log-
    |--20120825 -- error_log.txt
    |--20120826 -- 
    |--20120827 -- error_log.txt
    |--20120828 -- error_log.txt

如何foreach loop在其中创建一个名为的文件夹log,然后获取所有subfolder name并判断是否有error_log.txt这个 subfolder

如图folder table所示,里面20120826没有error_log.txt,所以不要打印folder name.

最后我需要获取文件夹名称:20120825, 20120827, 20120828

$folder = dirname(__FILE__) . '/../log/';
foreach(glob($folder) as $subfolder){
  if(file_exists($subfolder.'/error_log.txt')){
    echo  $subfolder.'<br />'; //I get nothing.
  }
}
4

3 回答 3

1
$folder = dirname(__FILE__) . '/../log/';
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder)) as $entry) {
  if ($entry->isFile()) {
    echo $entry, '<br />';
  }
}

Used in this way, you'll only get actual files. And if you don't like the iterator style, use:

$folder = dirname(__FILE__) . '/log/*';
foreach(glob($folder) as $subfolder){
  $path = $subfolder . '/error_log.txt';
  if (file_exists($path)) {
    echo basename($subfolder), '<br />';
  }
}
于 2012-08-28T13:50:25.683 回答
1

也可以使用scandir

$folder = dirname(__FILE__) . '/../log/';
$files = scandir($dir);
foreach($files as $subfolder){
  if(is_numeric($subfolder)){ // As your folder all use number, use `is_numeric` to remove sub folder '.' and '..'
    if(file_exists($folder.$subfolder.'/error_log.txt')){
      echo  $subfolder.'<br />';
    }
  }
}
于 2012-09-22T15:29:48.847 回答
1

使用以下

$folder = dirname(__FILE__) . '/../log';


foreach(glob($folder.'/*') as $subfolder){
  if(file_exists($subfolder.'/error_log.txt')){
    echo  $subfolder.'<br />'; //You will get the folder name
}
于 2012-08-28T13:59:30.340 回答