PHP解压zip文件
super
2021-02-04 18:42
2928
PHP7.1
function
/**
* 解压zip文件到指定目录
* @param string $filepath 文件路径
* @param string $extractTo 解压路径
* @return array
*/
function unZip(string $filepath, string $extractTo) :array
{
$zip = new \ZipArchive();
$res = $zip->open($filepath);
if ($res === TRUE) {
// 创建文件夹
mkdir(iconv("UTF-8", "GBK", $extractTo), 0777, true);
$zip->extractTo($extractTo);
$zip->close();
$data['state'] = 200;
$data['success_path'] = $extractTo;
} else {
$data['state'] = 500;
}
return $data;
}
example
// zip文件路径
$zip_path = $_SERVER['DOCUMENT_ROOT'] . '/upload/zip/test.zip';
// 解压目录
$decompress_path = $_SERVER['DOCUMENT_ROOT'] . '/upload/unzip/test/';
// 解压
$result = unZip($zip_path, $decompress_path);
if ($result['code'] == 200) {
// 删除压缩包
unlink($zip_path);
}
0 条讨论