创建一个zip文件并下载它

Har*_*jan 33 php zip download

我试图通过在本地服务器上创建zip文件来下载2个文件.该文件以zip格式下载但是当我尝试提取它时.它给出错误: 找不到中心目录签名.此文件不是zip文件,也不构成多部分存档的一个磁盘.在后一种情况下,中心目录和zip文件注释将在此存档的最后一个磁盘上找到.

我正在使用以下代码:

 <?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;
}




?>
Run Code Online (Sandbox Code Playgroud)

我检查了传入函数的所有变量的值,一切都很好.所以请看这个.谢谢.

Kub*_*tek 39

添加Content-length描述zip文件大小的标头,以字节为单位.

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");
Run Code Online (Sandbox Code Playgroud)

另外,还要确保绝对不会有空格之前<?和之后?>.我在这里看到一个空格:

 <?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
Run Code Online (Sandbox Code Playgroud)

  • 不,不是的.请参考http://php.net/manual/en/language.types.string.php变量在字符串中被替换,我不想更改原始代码(不要混淆OP). (3认同)

vit*_*ams 6

// 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);
Run Code Online (Sandbox Code Playgroud)

我在这里找到了这个解决方案,它对我有用


Avi*_*nav 6

错误之一可能是文件未读取为“存档”格式。检查ZipArchive 未打开文件 - 错误代码:19。在文本编辑器中打开下载的文件,如果开头有任何 html 标签或调试语句,请在读取文件之前清除缓冲区。

ob_clean();
flush();
readfile("$archive_file_name");
Run Code Online (Sandbox Code Playgroud)


M A*_*fan 6

$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!';
   }
Run Code Online (Sandbox Code Playgroud)


Dav*_*uez 5

我刚刚遇到这个问题。对我来说,问题在于:

readfile("$archive_file_name");
Run Code Online (Sandbox Code Playgroud)

这导致了内存不足错误。

Allowed memory size of 134217728 bytes exhausted (tried to allocate 292982784 bytes)
Run Code Online (Sandbox Code Playgroud)

我可以通过用以下内容替换 readfile() 来纠正问题:

    $handle = fopen($zipPath, "rb");
    while (!feof($handle)){
        echo fread($handle, 8192);
    }
    fclose($handle);
Run Code Online (Sandbox Code Playgroud)

不确定这是否是您的同一问题,还是没有看到您的文件只有 1.2 MB。也许这会帮助其他有类似问题的人。