首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >创建一个zip文件并下载它

创建一个zip文件并下载它
EN

Stack Overflow用户
提问于 2012-09-01 15:53:22
回答 7查看 133K关注 0票数 39

我正在尝试通过在本地服务器上创建压缩文件来下载a 2文件。该文件是以压缩格式下载的,但是当我尝试解压it.it时,出现错误:End-of-central directory signature not found。此文件不是zip文件,或者它构成了多部分归档的一张磁盘。在后一种情况下,中心目录和压缩文件注释将在此存档的最后一张磁盘上找到。

下面的代码是我用来做这个的:

代码语言:javascript
复制
 <?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');

//Archive name
$archive_file_name=$name.'iMUST_Products.zip';

//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';


zipFilesAndDownload($file_names,$archive_file_name,$file_path);

function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
        //echo $file_path;die;
    $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files,$files);
        //echo $file_path.$files,$files."

    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}




?>

我检查了传入函数的所有变量的值,都是fine.so,请提前查看this.Thanks。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2012-09-01 16:06:45

添加以字节为单位描述压缩文件大小的Content-length头。

代码语言:javascript
复制
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$archive_file_name");

还要确保在<?之前和?>之后绝对没有空格。我在这里看到了一个空格:

代码语言:javascript
复制
 <?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
票数 54
EN

Stack Overflow用户

发布于 2016-01-12 14:47:11

代码语言:javascript
复制
$zip = new ZipArchive;
$tmp_file = 'assets/myzip.zip';
    if ($zip->open($tmp_file,  ZipArchive::CREATE)) {
        $zip->addFile('folder/bootstrap.js', 'bootstrap.js');
        $zip->addFile('folder/bootstrap.min.js', 'bootstrap.min.js');
        $zip->close();
        echo 'Archive created!';
        header('Content-disposition: attachment; filename=files.zip');
        header('Content-type: application/zip');
        readfile($tmp_file);
   } else {
       echo 'Failed!';
   }
票数 10
EN

Stack Overflow用户

发布于 2015-06-14 06:40:25

代码语言:javascript
复制
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);

我发现这个解决方案here,它对我很有效

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12225964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档