繁体   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);

?>

这是我的代码,唯一的问题是它没有下载我指定的文件。

难道我做错了什么? 我正在使用的浏览器吗? (歌剧)

文件下载,但最终为空。

首先检查zip文件是否正在创建,然后,

添加文件传输头...

        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);

它会工作。

将“您的文件大小”附加到文件大小的长度。

暂无
暂无

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

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