I have some error_log.txt
files, create in /var/www/html/log/'.date(Ymd).'/error_log.txt
, The folder table like this.
-log-
|--20120825 -- error_log.txt
|--20120826 --
|--20120827 -- error_log.txt
|--20120828 -- error_log.txt
How to make a foreach loop
in folder which name as log
, then get all the subfolder name
and make a judgment if there has error_log.txt
in this subfolder
?
As the folder table
show, 20120826
have no error_log.txt
in it, so do not print the folder name
.
Finally I need get the 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.
}
}
$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 />';
}
}