Last active
February 28, 2020 10:32
-
-
Save GithubMrxia/2b6e5a0382734434576f9f9d148faddb to your computer and use it in GitHub Desktop.
php解压zip文件并把所有图片放到制定目录下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment