php作文件下載主要用到一下幾行代碼
php
header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="/tmp/abc.pdf"'); header('Content-Length: '.filesize('/tmp/abc.pdf')); header('Content-Transfer-Encoding: binary'); readfile('/tmp/abc.pdf');
須要注意的是,若是文件名中有中文,filesize(),file_exists(),readfile()都不會反回預期結果,文件會下載,只是下載的文件大小是0,此時在將文件路徑傳給以上幾個函數前,須要對路徑中的中文進行轉碼:app
$filename=iconv('UTF-8','GB2312',$filename);
轉碼以後一切正常函數