我有一個問題,迫使PHP下載一個文件,當我運行這段代碼:強制PHP下載文件時,顯示未知符號
$file = "storage/".$filename;
header('Content-Description: File Transfer');
header('Content-Type: '.$mimetype);
header('Content-Disposition: attachment; filename='.basename($originalfilename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
而不是迫使瀏覽器下載文件時,它顯示文件的全部內容。 例如,當我想強制瀏覽器下載test.png時,瀏覽器本身會顯示大量未知符號(exp。 q M 6 %V һK..)。我如何強制瀏覽器下載文件而不是顯示它?
我用活HTTP頭,當我在此頁面點擊下載按鈕: http://www.helios.ir/get.php?file=17/dl.php?name=56379948c6df5e49d3073aee14358e3fc98ba5ae.jpg
它顯示了這個標題:
----------------------------------------------------------
http://www.helios.ir/download.php?file=17.jpg&name=56379948c6df5e49d3073aee14358e3fc98ba5ae.jpg
GET /download.php?file=17.jpg&name=56379948c6df5e49d3073aee14358e3fc98ba5ae.jpg HTTP/1.1
Host: www.helios.ir
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Referer: http://www.helios.ir/get.php?file=17/dl.php?name=56379948c6df5e49d3073aee14358e3fc98ba5ae.jpg
Cookie: mfh_mylang=en; mfh_sess_id=effccfb2429a54b6f26ef2f155144c98; mfh_logined=0; mfh_uid=0; mfh_last_click=1345470635; __utma=44342077.1613075140.1345761327.1345912829.1345927090.14; __utmz=44342077.1345910409.12.11.utmcsr=localhost|utmccn=(referral)|utmcmd=referral|utmcct=/share6/get.php; __utmb=44342077.18.10.1345927090; PHPSESSID=bb18789f8011836092f4e2d14aa3118d; __utmc=44342077
HTTP/1.1 200 OK
Date: Sat, 25 Aug 2012 22:25:01 GMT
Server: Apache/2.2.22 (CentOS)
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
你的代碼看起來不夠理智。 '$ mimetype'的價值是什麼,因爲這是告訴瀏覽器你給的是什麼類型的文件。你是否收到任何錯誤或警告? – IMSoP
你可以發佈你正在發送的標題嗎?否則,將很難給出有意義的答案... – CAFxX
**請**發佈您正在發送的標題。 – CAFxX