简体   繁体   English

php zipfile返回一个空的zip文件

[英]php zipfile returning an empty zip file

<?php

$files = array('1.txt', '2.txt', '3.txt');
$zipname = 'test.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

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

?>

this is my code, the only issue is it is not downloading the files i have specified. 这是我的代码,唯一的问题是它没有下载我指定的文件。

am i doing something wrong? 难道我做错了什么? is it the browser im using? 我正在使用的浏览器吗? (opera) (歌剧)

the file downloads but it ends up being empty. 文件下载,但最终为空。

First check zip file is creating or not and then, 首先检查zip文件是否正在创建,然后,

Add File transfer header... 添加文件传输头...

        header('Content-Description: File Transfer');
        header('Content-Type: application/zip');
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' ."Your file size" );            
        set_time_limit(0);

It will work. 它会工作。

Append "Your file size" to the length of the file size. 将“您的文件大小”附加到文件大小的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM