开发者

How to download a zipped file in php? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to zip a whole folder using PHP

Hi, I am creating a zip folder using zipArchive in php.The zipped file contains a folder(with a file in it) and a file. I am using the following code to download the zipped file:

$file_path = "zip/test.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=download.zip');
header('Content-Length: ' . filesize( $file_path));
readfile($file_path);

The folder gets downloaded. But I am unable to unzip it. The error specified is "Decompression failed". I can download a single file with the same code but not a file within a folder. Please help.. Here is the code to zip the files:

$zip = new ZipArchive;
if ($zip->open($file_path,ZIPARCHIVE::CREATE) === TRUE) {

   if($z开发者_StackOverflowip->addEmptyDir('dir1')) {
       $zip->addFile($filepath,$destinationPath);
       if($zip->addEmptyDir('Files')) {
           $zip->addFile($filepath2,$destinationPath2);
       } 
   }
}


I got the code working... Need to add a $zip->close() code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜