function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false //如果zip文件已经存在并且设置为不重写返回false if(file_exists($destination) && !$overwrite) { return false; }else{ $overwrite=false; } //vars $valid_files = array(); //if files were passed in... //获取到真实有效的文件名 if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... //如果存在真实有效的文件 if(count($valid_files)) { //create the archive $zip = new ZipArchive(); //打开文件 如果文件已经存在则覆盖,如果没有则创建 if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files //向压缩文件中添加文件 foreach($valid_files as $file) { $zip->addFile($file,basename($file)); //第一个参数是去取得路劲 第二个参数是压缩文件里的名字 } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! //关闭文件 $zip->close(); //check to make sure the file exists //检测文件是否存在 return file_exists($destination); }else{ //如果没有真实有效的文件返回false return false; } }
$files=array( "C://a/a/headimg_2.jpg", "C://a/a/headimg_3.jpg" ); $res=create_zip($files, "D://hfzip/".time().".zip", true);