[archive zip file] php解压zip文件并把所有图片放到制定目录下 #php #zip #archive
<?php
$za = new ZipArchive();
$res = $za->open('test.zip');
if (!$res) {
return $this->respApiRequestFail('解压失败');
}
$path = storage_path('/path/to');
for ($i = 0; $i < $za->numFiles; $i++) {
$fileInfo = $za->statIndex($i);
$fileName = basename($fileInfo['name']);
$fileArr = explode('.', $fileName);
$fileExtension = end($fileArr);
if (!$fileExtension || !in_array($fileExtension, ['jpg', 'jpeg', 'png'])) {
continue;
}
$fileContent = $za->getStream($fileInfo['name']);
file_put_contents($path . '/' . $fileName, $fileContent);
}
$za->close();