Page 1 of 1
Don't open it.. download it!
Posted: Tue Dec 16, 2003 4:54 pm
by Gen-ik
Is there a way I can force a file to download rather than be loaded up the browser?
Code: Select all
An example might be..
<a href="piccy.gif">Download piccy.gif</a>
..but I would like to download the file rather than having it load into the browser.
Code: Select all
I'm guessing I might need to route the file through a PHP script with something like..
<a href="force_download.php?file=piccy.gif">Download piccy.gif</a>
..but I'm not too sure of the header info I might need to use.
Any ideas would be great.
Thanks.
Posted: Tue Dec 16, 2003 5:03 pm
by microthick
Using .htaccess in a folder containing those images, you might be able to trick the server into serving up .gif's as it would a .zip or something. Similar to how you can use .htaccess to have .inc files interpret as .php.
Other than that, all I can suggest is that you find a method of automating the process of zipping up that .gif and then serving up the .zip file instead.
Posted: Tue Dec 16, 2003 6:18 pm
by DuFF
A little Google search found the answer pretty quick

:
Assuming $file = name of file
Code: Select all
<?php
$myFileHandler = fopen($file, "r");
$myFileContents = fread($myFileHandler, filesize($myFile));
header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: Application/octet-stream");
header("Content-disposition: attachment; filename=" . $file);
echo $myFileContents;
?>
Posted: Tue Dec 16, 2003 6:27 pm
by Gen-ik
DuFF
Cheers mate. I tried Google but didn't find anything.. must have been looking for the wrong thing.
Thanks, I'll give it a whirl.
Posted: Tue Dec 16, 2003 6:34 pm
by Gen-ik
Doh. It nearly works.
Just need to work out how to keep the .GIF extension on the download. At the moment it's being replaced with .PHP
Maybe a bit of .htaccess might work to sort this out. I'll keep you posted.