0

I want to save an image from an URL to the user's local machine in PHP. Is this possible? I've been researching for a while, and I can't seem to find the answer. This is my code

function Save()
{
   header('Content-Type: image/png');
    header('Content-Disposition: attachment; filename="thumbnail.png"');
    imagepng("url?");
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Cene Jenko
  • 23
  • 7
  • 1
    Possible duplicate of [Saving image from PHP URL](http://stackoverflow.com/questions/724391/saving-image-from-php-url) – CodeGodie Mar 10 '17 at 22:56
  • No, that saves the image to the website server. – Cene Jenko Mar 10 '17 at 22:57
  • True. Sounds like youll need JS for that. – CodeGodie Mar 10 '17 at 23:01
  • 1
    Possible duplicate of [How to force file download with PHP](http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) – Zoe Edwards Mar 11 '17 at 00:21
  • This is not possible at all in a browser environment - allowing a website arbitrary write access to your disk would be a security nightmare. You can do it if the user in question has PHP on their own machine and runs your script, but that is a very different thing. – halfer Mar 11 '17 at 10:30

1 Answers1

0

If there is a download link that the user clicks on, you can have it re-direct to the url of the file you want to serve directly, the client browser should handle the rest.

If the file needs to be re-served from your system having a php file that sets those headers and then echo's the imagepng output should do it.

Patrick Kelly
  • 297
  • 2
  • 9