我有一个文件夹 store_files ,我有多个子文件夹。现在我可以获取所有文件夹和所有文件。现在我想获取所有文件夹,除了 zip 文件之外别无他物。我尝试添加条件,如果文件扩展名等于 zip 但似乎不起作用。如果我添加 $ext == 'zip' 输出中不会显示任何内容。有没有另一种方法来获取特定文件,例如在图像中有一个glob($getDirectory. '*.zip')
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
$ext = pathinfo($ff, PATHINFO_EXTENSION);
if($ff != '.' && $ff != '..' && $ext == 'zip'){
echo '<li>';
$str = preg_replace('/\D/', '', $ff);
$str = substr($str, 0,-1);
$getYear = substr($str, 0,4);
$getMonth = substr($str, -4,2);
$getDay = substr($str, -2,2);
$theDate = $getYear.'-'.$getMonth.'-'.$getDay;
$theRealDate = date('Y M j', strtotime($theDate));
echo $theRealDate;
if(is_dir($dir.'/'.$ff)){
listFolderFiles($dir.'/'.$ff);
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('store_files');
我认为您的问题源于这样一个事实,scandir()
即(本身)不是递归目录搜索,它仅直接在指定目录中列出文件和目录。listFolderFiles()
如果您检测到它$ff
是一个目录,那么您已经在递归调用您的函数时考虑到了这一点。但是,您的问题是您已经在之前的 if 语句中过滤掉了目录(假设您的目录不以“.zip”结尾)。我认为以下功能是您所追求的:
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
$ext = pathinfo($ff, PATHINFO_EXTENSION);
if($ff != '.' && $ff != '..' && (is_dir($dir.'/'.$ff) || $ext == 'zip')){
echo '<li>';
$str = preg_replace('/\D/', '', $ff);
$str = substr($str, 0,-1);
$getYear = substr($str, 0,4);
$getMonth = substr($str, -4,2);
$getDay = substr($str, -2,2);
$theDate = $getYear.'-'.$getMonth.'-'.$getDay;
$theRealDate = date('Y M j', strtotime($theDate));
echo $theRealDate;
if(is_dir($dir.'/'.$ff)){
listFolderFiles($dir.'/'.$ff);
}
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('store_files');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句