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

为什么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!

图片转代码服务由CSDN问答提供 功能建议

我搜索并尝试了一些以前的问题/示例代码,但无法使其正常工作。

我正在尝试通过PHP代码将结果传递给最终用户。 这是我的代码。

  $ 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();  //标记行xxx 
 
标题(“Content-Type:application / zip”); 
标题(“Content-disposition:attachment; filename = $ varZipFile”); 
标题('Content-Length:'  .cilesize($ varZipFile));  //有或没有这一行,得到了相同的错误
标题(“Content-Type:application / force-download”);  //有或没有这一行,得到了同样的错误
 readfile($ varZipFile); 
   
 
 

我的浏览器中有.zip文件。 但WinZip无法打开它,也不能7-Zip。 WinZip抱怨“错误:未找到中心目录”。

有趣的是,当我通过WinSCP从我的服务器手动传输文件到我的Windows机器时,我可以使用WinZip或WinZip打开文件 7-Zip的。 这表明它可以很好地“标记行xxx”,并且标题行中出现问题。

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();
    
    点赞 打赏 评论
  • duanlv2788 2017-04-27 21:07

    Your code is working well on my side, debug this $varBaseNameParts[0] to see if the value is correct or try the below code.

    // $name = name of the archive
    // $archive_directory = directory to save the archive
    // $file_array = list of files
    function create_archive($name, $archive_directory, $file_array) {
        $target_file = $archive_directory . '/' . $name . date('m-d-y').date('h-i-sa') . '.zip';
        $zip = new ZipArchive();
        if (count($file_array)) {
            if ($zip->open($target_file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
                foreach ($file_array as $file) {
                    $zip->addFile($file);
                }
                $zip->close();
                chmod($target_file, 0777);
                return $target_file;
            }
        }
        return null;
    }
    
    function create_download($archive) {
        $file_name = basename($archive);
        header("Content-Type: application/zip");
        header("Content-Disposition: attachment; filename=$file_name");
        header("Content-Length: " . filesize($archive));
    
        readfile($archive);
    }
    
    // create the archive file
    $archive = create_archive('test', 'archive', ['008.csv', '002.csv']);
    if ($archive) {
        create_download($archive);
    }
    
    点赞 打赏 评论