Simon Davies
Simon Davies

Reputation: 3686

PHP: Download an image via its url and prompt user to saveit

Whats the best what to enable a user to click a download button that then automatically gets a image file and then downloads it to their generic downloads folder or prompts them to save it?

i have tried the following but with no success:

  $file = $art[0]['location']; // this is the full url to image http://....image.jpg
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_POST, 0); 
  curl_setopt($ch,CURLOPT_URL, $file); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $file_content = curl_exec($ch);
  curl_close($ch);

ect or am i going about this all wrong (probably)

thanks

Upvotes: 0

Views: 1044

Answers (2)

jimp
jimp

Reputation: 17487

header('Content-Disposition: attachment; filename="exmaple.jpg"');
header('Content-Type: application/octet-stream'); // (or a MIME type like "image/jpeg")

echo $file_content;

Upvotes: 0

Eric
Eric

Reputation: 97571

Before you echo the data:

header('Content-Disposition: attachment; filename=save_as_name.jpg');

Then

echo file_get_contents("http://...");

Upvotes: 5

Related Questions