header() 函數向客戶端發送原始的 HTTP 報頭。php
下載文件要用的的請求頭:瀏覽器
header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Accept-Length:" . $file_Size); header("Content-Disposition:attachment;filename=" . $filename);
文件名是中文的話,須要注意轉碼app
$filename = iconv("UTF-8", "GB2312", $filename);函數
具體實現代碼以下:code
$file = $_GET['file']; if(file_exists($file)){ header("Content-type:application/octet-stream"); $filename = iconv("UTF-8", "GB2312", basename($file)); header("Content-Disposition:attachment;filename=" . $filename); header("Accept-ranges:bytes"); header("Accept-length:".filesize($file)); readfile($file); } else { echo "<script>alert('文件不存在')</script>"; }