php 解壓檔案與壓縮檔案
阿新 • • 發佈:2018-11-16
配置環境變數
然後cmd 輸入 php -m
檢視是否有zip 選項 沒有的話zip功能將無法使用
function zip_file(string $filename) { if (!is_file($filename)) { return false; } $zip = new ZipArchive(); $zipName = basename($filename) . '.zip'; //開啟指定壓縮包,不存在則建立,存在則覆蓋 if ($zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { //將檔案新增到壓縮包中 if ($zip->addFile($filename)) { unlink($filename); } $zip->close(); return true; } else { return false; } } /* * 多檔案壓縮 */ function zip_files(string $zipName, ...$files) { //檢測壓縮包名稱是否正確 $zipExt = strtolower(pathinfo($zipName, PATHINFO_EXTENSION)); if ("zip" !== $zipExt) { return false; } $zip = new ZipArchive(); if ($zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { foreach ($files as $file) { if (is_file($file)) { # $zip->addFile($file); //將檔案新增到壓縮包中 if ($zip->addFile($file)) { unlink($file); } } } return true; } else { return false; } $zip . close(); } /** * 解壓縮 * @param string $zipName * @param string $dest * @return bool */ function unzip_file(string $zipName, string $dest) { //檢測要解壓壓縮包是否存在 if (!is_file($zipName)) { return false; } //檢測目標路徑是否存在 if (!is_dir($dest)) { mkdir($dest, 0777, true); } $zip = new ZipArchive(); if ($zip->open($zipName)) { $zip->extractTo($dest); $zip->close(); return true; } else { return false; } }