I want the user to be able to download some files I have on my server, but when I try to use any of the many examples of this around the internet nothing seems to work for me. I've tried code like this:
<?php $size = filesize("Image.png"); header('Content-Description: File Transfer'); header('Content-Type: image/png'); header('Content-Disposition: attachment; filename="Image.png"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $size); readfile("Image.png");
I've even tried to use the most basic example I could find, like this:
<?php header('Content-type: image/png'); header('Content-Disposition: attachment; filename="Image.png"'); readfile('Image.png');
When I've tested this I have removed all the other code I have and used an empty file with just this code to remove any faults created by external sources.
When I look in the console the file gets sent with the right headers i.e
'Content-Disposition: attachment; filename="Image.png"'
But the save dialog isn't displayed.
I've also tried with inline instead of attachment in the content disposition header but that didn't make a difference either, I've tested this in Firefox 8.0.1 Chrome 15.0.874.121 and Safari 5.1.1.
For force download, you have to set the header correctly. When you are setting the header for download the Zip file that time <coce>Content-type and Content-Disposition correctly. In Content-Disposition , add the attachment, it will suggest the browser to download the file instead of displaying it directly.
I’m pretty sure you don’t add the mime type as a JPEG on file downloads:
header('Content-Type: image/png');
These headers have never failed me:
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\')); $size = filesize($file); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $quoted); header('Content-Transfer-Encoding: binary'); header('Connection: Keep-Alive'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $size);
This worked for me like a charm for downloading PNG and PDF.
header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.$file_name.'"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file_url)); //Absolute URL ob_clean(); flush(); readfile($file_url); //Absolute URL exit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With