Zipped file with PHP results in cpgz file after extraction

后端 未结 1 1627
生来不讨喜
生来不讨喜 2021-01-05 11:27

I\'m zipping folders and files with php, but when I try to open up the zip file I get an cpgz file instead. After extracting that file I get another zip file. What it does i

相关标签:
1条回答
  • I was just having exactly the same issue, and learned that these two function calls can help:

    header("Content-disposition: attachment; filename=$filename");
    header('Content-type: application/zip');
    
    // Add these
    ob_clean();
    flush();
    
    readfile($filename);
    unlink($filename);
    

    Usually setting Content-Disposition and Content-Length should be enough, however flushing PHPs output buffer and the underlying output buffer (Apache or such) helps when output is accidentally sent before headers are set in PHP.

    In my case, commenting out the header() and readfile() calls for debugging helped to see, that warnings were output before the file was sent.

    Hope that helps somebody in the future.

    0 讨论(0)
提交回复
热议问题