The content of this article is about the implementation of the PHP single file compression function. The correct writing method of recursion has a certain reference value. Now I share it with everyone. Friends in need can refer to it
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; } }
Related recommendations:
php function to implement decompression function
The above is the detailed content of Implementation of PHP single file compression function. For more information, please follow other related articles on the PHP Chinese website!