Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force file download with php using header()

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.

like image 697
Kerp Avatar asked Dec 13 '11 07:12

Kerp


People also ask

How can I force download a zip file in PHP?

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.


2 Answers

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); 
like image 120
Lawrence Cherone Avatar answered Sep 28 '22 21:09

Lawrence Cherone


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(); 
like image 29
Farhan Sahibole Avatar answered Sep 28 '22 19:09

Farhan Sahibole