I have a simple question. I was looking for function to download file. I have found one but I have small problem.
First, this is some codes of function:
header ('Content-type: ' . mime_content_type($file));
header ('Content-Disposition: attachment; filename="'.basename($file).'"');
header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT');
header ('Accept-Ranges: bytes');
header ('Cache-control: no-cache, must-revalidate');
header ('Pragma: private');
To test function I am using this:
$file = 'path/file.rar';
dowload_file($file);
It’s working fine and file is getting downloaded. But if I use the function for image, for example:
$file = 'path/image.jpg';
download_file($file);
Then the image is also getting downloaded. But I want it to be viewed and not downloaded.
If I delete this line:
header ('Content-Disposition: attachment; filename="'.basename($file).'"');
the image is showed immediately but the rar and zip files is stopped being downloaded.
So how to make images being displayed without downloading but any other file being downloaded? I am testing on localhost, may be this is the problem?
If you have the
Content-Dispositionheader set toattachment, it will force a download regardless of file type.If you want your images to be handled differently, you’ll have to include a conditional on the
Content-Dispositionheader, like: