php實(shí)現(xiàn)大文件下載且隱藏文件真實(shí)地址

傳統(tǒng)的處理文件下載隱藏真實(shí)地址,相信很多小伙伴都能夠在網(wǎng)上找到以下代碼:

header('Content-Description: File Transfer');
 
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0′);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($file_path);

代碼確實(shí)能下載文件,但是,當(dāng)文件過大的時(shí)候,就會出現(xiàn)意想不到的錯(cuò)誤:

  • 下載的文件莫名變小
  • 下載的文件網(wǎng)絡(luò)錯(cuò)誤

以下為大文件正常下載代碼:


$filesize=filesize($file)+1000;
 header('Content-Description:File Transfer');
 header("Content-Type:application/octet-stream");
 header('Content-Transfer-Encoding:binary');
 header("Accept-Ranges: bytes");
 header('Expires:0');
 header('Cache-Control:must-revalidate');
 header('Pragma:public');
 header("Content-Length:".$filesize);
 header("Content-Disposition:attachment;filename=".$file_naked);
$fp = fopen($file, "rb"); 
fseek($fp,0); 
while (!feof($fp)) { 
    set_time_limit(0); 
    print (fread($fp, 1024 * 8)); 
    flush(); 
    ob_flush(); 
} 
fclose($fp); 
exit ();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容