使用PHP內建ZipArchive壓縮、解壓縮


 

壓縮檔案

$zip = new ZipArchive;
if ($zip->open('./temp/test.zip', ZipArchive::OVERWRITE) === TRUE) {
    $zip->addFile('./temp/Forcast_SP3_20210813150517.xls');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

解壓縮檔案

$zip = new ZipArchive;
if ($zip->open('./temp/test.zip') === TRUE) {
    # 解壓縮至特定資料夾
    $zip->extractTo('unzip');
    $zip->close();
    echo 'OK';
} else {
    echo 'failed';
}


留言