Web Development Blog

PHP, allow visitor to download image that is not on your server

 We posted web development guide with the information on how to save remote images from the web using PHP, and one of the web developers asked us a question on how you can allow people to download the image without saving it on the server, and here is the answer to this question.

Here is the link to the previous post: http://superiorwebsys.com/84-php-how-to-save-image-from-web-remote-image/

Here is the very simple code that will allow you to do so:

<?
    $remoteFile
='http://chart.apis.google.com/chart?chs=150x150&cht=qr&chld=L|0&chl=http://superiorwebsys.com'
    
header("Content-Type: application/force-download");
    
header("Content-Disposition: attachment; filename=\"QR.jpg\";" );
    
header("Content-Transfer-Encoding: binary");
    
readfile($remoteFile);
?>

 Click here to run thgis code

In place of $remoteFile you can put a link to any file and this script will force user browser to offer downloading of the content instead of opening it .

 

Associated tags:  Web Development, PHP, Code, Save Image

Comments:

Michael Pankratov wrote on March 9, 2012 at 15:56
Hello, Here is instructions on how you can force download image that is not located on your server: http://superiorwebsys.com/96-php-allow-visitor-to-download-image-that-is-not-on-your-server/

Bike wrote on March 11, 2012 at 17:31
My partner and I stumbled over here from a different page and thought I might as well check things out.
I like what I see so i am just following you. Look forward to looking into your web page yet again.

Zaleha wrote on March 27, 2012 at 02:43
hey there and thank you for your info e2€“ Ie2€™ve certainly pkeicd up something new from right here. I did however expertise some technical issues using this web site, since I experienced to reload the site many times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I`m complaining, but sluggish loading instances times will often affect your placement in google and could damage your high-quality score if ads and marketing with Adwords. Well I am adding this RSS to my email and can look out for much more of your respective interesting content. Make sure you update this again soon..

Nick Georgiou wrote on April 12, 2012 at 00:39
Thanks for this! What are the security implications of this method??

Roger wrote on April 26, 2012 at 08:21
I tried your code by just copy and paste but it wont work. The image file is generated but without any QR code. but when I tried your demo it works. Is there something else that you missed out in this tutorial?

Michael Pankratov wrote on April 26, 2012 at 11:55
Hello Roger,
I just tried that code again and it did work fine for me. Do you get any warnings?

Roger wrote on April 27, 2012 at 02:27
Hi Michael,
I do not get any warning & even I get to download the QR.jpg. But when I try to open, it cannot. The downloaded file size is 130 bytes; no matter what `chl` parameter i provide

Roger wrote on April 28, 2012 at 03:49
Hello Michael,
I have got the code working. Don`t know the reason why it was not working but at last got it working. First used CURL to get the image saved on a server space and then did the download part. Would have shared the code here but don`t know if you would permit that. :)
Thanks for all the support.

Michael Pankratov wrote on April 30, 2012 at 09:44
Hello Roger,
I`m glad you were able to figure out the problem. You can share the code if you want here, and we will modify styles so it looks like a code.
Thank You!


thomas wrote on August 8, 2012 at 03:26
Very cool! Thank you!


Add Comment:

CAPTCHA