I'm trying to force the browser to bring up a 'save as...' dialog box when a download link is clicked which is linked to a jpg image file, instead of opening the image in the browser.
After searching the net, I found the same snippet of code all over the place for doing this, but it won't work properly for me. Here's how the href link looks for a given image file:
<a href="image.php"><img src="images/save.gif" alt="save"></a>
So when the link is clicked, it references the image.php file, inside which is the following:
<?php header('Content-disposition: attachment; filename=mypicture.jpg'); header('Content-type: image/jpeg'); readfile('images/mypicture.jpg'); ?>
Now, I know that there can be no blank lines after the php code, or garbage may be added to the image file as it's rebuilt. The problem is that although this works and brings up a save as box, when you save the file, it's a 0Kb file.
It's the same behaviour as happens if you tell it to use a file that doesn't exist in the 'readfile' area. But I know the file is there and the address for the image file is correct and is how it's used on the rest of the site. I have the root directory on the server, with the "images" folder in the root, so image links are usually simply a href=images/whatever.jpg.
Can anyone help shed light on what's going wrong please?