PHP readfile() causing corrupt file downloads

前端 未结 3 800
野趣味
野趣味 2020-12-05 15:34

I am using php script to provide download from my website after a requisite javascript timer this php script is included which causes the download. But the downloaded file i

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 16:11

    This helped me in case of more output buffers was opened.

    //NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
    header('Content-type: application/octet-stream');
    header('Content-Length: ' . filesize($f));
    //This would be the one to rename the file
    header('Content-Disposition: attachment; filename='.$n.'');
    //clean all levels of output buffering
    while (ob_get_level()) {
        ob_end_clean();
    }
    readfile($f);
    exit();
    

提交回复
热议问题