I have a problem with forcing php to download a file, when i run this code :
$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();
Instead of forcing the browser to download the file, it show all content of file.
For example when i want to force the browser to download test.png, browser shows alot of unknown symbols (exp. �q�M�6�%V�һK.. ) in itself. How can i force the browser to download the file instead of showing it?
I used Live HTTP Headers and when i click the download button in this page:
http://www.helios.ir/get.php?file=17/dl.php?name=56379948c6df5e49d3073aee14358e3fc98ba5ae.jpg
It shows this headers:
----------------------------------------------------------
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
----------------------------------------------------------
In the response headers you just posted no
Content-Disposition: attachmentappears… You should probably drop theob_clean()call (or, at the very least, move it before theheader()calls*).Also, that
flush()is completely pointless (what’s the point of flushing an empty buffer?). Remove it.*provided you called
ob_start()beforehand – typically at the start of your script