PHP readfile() causing corrupt file downloads

前端 未结 3 791
野趣味
野趣味 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:10

               ob_start();//add this to the beginning of your code 
    
          if (file_exists($filepath) && is_readable($filepath) ) {
    header('Content-Description: File Transfer');
     header("Content-Type: application/octet-stream");
     header("Content-Disposition: attachment; filename=$files");
     header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
       header('Pragma: public');
     header("content-length=".filesize($filepath));
     header("Content-Transfer-Encoding: binary");
    
       /*add while (ob_get_level()) {
           ob_end_clean();
             }  before readfile()*/
      while (ob_get_level()) {
    ob_end_clean();
       }
        flush();
       readfile($filepath);
    

提交回复
热议问题