如果您正在寻找一个简单的 PHP 函数来列出文件和文件夹而不是子文件夹,这里有一个简单的精心设计的函数,您可以对其进行测试,看看它是否适合您。
function unzip($path){
if($path === null ) return 'File not found.';
$zip = new ZipArchive();
$entryList = '<ul class="list-group"> ';
if ($zip->open($path) == TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
//set the prerequisit inputs
$stat = $zip->statIndex($i); //an array of file statistics or details
$filename = $stat['name']; // entry name
$size = $stat['size']; //entry size
//list only folders and file names but not subfolders.
//when size is zero,its folder, list it, and when entry name
//doesn't contain the (/), its is a file, list it.
$isFile = strstr($filename, '/') === false;
$anyFile = preg_match('/(\..+)$/',$filename);
$icon = ($anyFile ? 'fa-file text-info' : 'fa-folder text-warning' );
if($size == 0 || $isFile ){
$filename = str_replace('/','',$filename);
$entryList .='<li class="list-group-item ">';
$entryList .= '<i class="fa '.$icon.' mr-1 "></i>';
$entryList .='<button class="btn btn-link">'.$filename.'</button></li>';
}
}
$zip->close();
}
return $entryList.'</ul>' ;
}
这是输出: