当前位置: 动力学知识库 > 问答 > 编程问答 >

php - ZIP file corrupted when downloaded by header

问题描述:

This question already has an answer here:

  • Download of .zip file runs a corrupted file php

    5 answers

网友答案:

Try add exit(); on the end of script. It is possible that script send inadvertently whitespace after commands readfile(), for example whitespace after the ?>

It is also suitable to use ob_start() ob_clean() functions.

    ob_start();

    // .... some code

    header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Length: ".filesize($zip_name));
    header("Content-Disposition: attachment; filename=\"".$zip_name."\"");

    // disable cache
    header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header("Cache-control: private");
    header('Pragma: private');

    ob_end_clean();
    readfile($zip_name);

    exit();      
分享给朋友:
您可能感兴趣的文章:
随机阅读: