doufu2396 2017-04-27 20:41
浏览 94
已采纳

为什么zip文件在下载后无法打开但是通过手动传输很好?

I searched and tried a few previous questions/example codes, but couldn't get this to work.

I'm trying to deliver the results to end-users via PHP code. Here is my code.

       $varZipFile = $varBaseNameParts[0] . '.' . $varDate . '.zip';
       $varZipDir = $varBaseNameParts[0] . '_' . $varDate;

       $zip = new ZipArchive();
       $zip->open($varZipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
       $zip->addFile('008.csv');
       $zip->addFile('002.csv');
       $zip->close(); // mark line xxx

       header("Content-Type: application/zip");
       header("Content-disposition: attachment;filename=$varZipFile");
       header('Content-Length: ' . filesize($varZipFile)); // with or without this line, got the same error 
       header("Content-Type: application/force-download"); // with or without this line, got the same error 
       readfile($varZipFile);

I got the .zip file in my browser. However WinZip cannot open it, neither can 7-Zip. WinZip complains that "Error: Central directory not found".

Interestingly, when I manually transfer the file via WinSCP from my server to my Windows machine, I can open the file with either WinZip or 7-Zip. This indicates it works all fine to 'mark line xxx', and problems occurs in the header lines.

TIA!

  • 写回答

2条回答 默认 最新

  • dongniuxia8650 2017-04-27 21:16
    关注

    Your ouput buffer may not be cleared before you try to serve the download. Try to clean the output buffer before serving the download with the ob_clean() function like this:

    $zip->close(); // mark line xxx
    ob_clean();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程