PHP頭文件下載文件的小坑

    function downLoad(){
           $path = './static/zhan/uploads/head/201708311617012941.png';
           $filename = '我的文件.png'; // 文件名
           //下面是輸出下載;
           header("Cache-Control: max-age=0");
           header("Content-Description: File Transfer");
           header('Content-disposition: attachment; filename=' . basename($filename )); // 文件名
           header('Content-Type: application/octet-stream');   //設置爲一個下載類型  
           header("Content-Transfer-Encoding: binary"); // 告訴瀏覽器,這是二進制文件
           header('Content-Length: ' . filesize($path)); // 告訴瀏覽器,文件大小
           @readfile($path); //輸出文件;
       }

這段代碼測試過正常的,之前
$path ='http://xxx/./static/zhan/uploads/head/201708311617012941.png'; 是寫成這樣的,導致下載下來的文件無法正常打開,之前以爲是文件大小原因,最後發現我把header('Content-Length: ' . filesize($path));這句註釋掉,居然可以正常打開文件了,最後才知道filesize($path)不需要加域名

其他header函數之設置content-type

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章