0

I am trying to force a php download. from here: http://www.iwantanimage.com/animals/animals01.html. Click on the sterling image & the next page offers the options of three formats.

this is my php code

<?php
header('Content-disposition: attachment; filename=sterling02md.jpg');
header('Content-type: image.jpg');
readfile('sterling02md.jpg');

header('Content-disposition: attachment; filename=sterling02lg.jpg');
header('Content-type: image.jpg');
readfile('sterling02lg.jpg');

header('Content-disposition: attachment; filename=sterling.jpg');
header('Content-type: image.jpg');
readfile('sterling02.jpg');
?> 

The only image that downloads however is the sterling02md.jpg. How do I fix the code so the user can download the file of choice? thank you

3 Answers3

1

Your content-types are incorrect. You have to provide the file's mime type, which would be image/jpeg. not image.jpg.

As well, you cannot force a download of 3 separate files in a single HTTP request. While some browsers do support multiple files, you must encapsulate each one in a separate MIME body block, which you are not doing.

Either provide a .zipped copy of these 3 files for a single download, or provide 3 separate download links, one file per link.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Hello Marc and thanks for replying. As you may guess I am clueless about php. I got the code above from the net. How do I "encapsulate each one in a separate MIME body block"? – pdxDaniela Oct 13 '12 at 17:50
  • it's not worth the trouble to do so. not all browsers support it. – Marc B Oct 13 '12 at 19:14
0

Set the choice in the query string download.php?option=1

if (!isset($_REQUEST['option']) {
   //redirect away
}
if ($_REQUEST['option'] == 1) {
   header(...)
   //sterling02md.jpg
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You can try something like where the links to the image downalod.php?img=sterling02

$image = isset($_GET['img']) ? $_GET['img']  : "noimage" ;
$image .= ".jpg";

header('Content-disposition: attachment; filename='.$image);
header('Content-Type: image/jpeg');
readfile($image);

But if you want to force the download then use

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Transfer-Encoding: binary ");
header('Content-disposition: attachment; filename='.$image);
readfile($image);
Baba
  • 94,024
  • 28
  • 166
  • 217