jamiet757 Posted April 2, 2010 Share Posted April 2, 2010 I need some help with header(). I am trying to make it so an image will be downloaded when the user loads the php file. I can figure out header("location:image.jpg") but here are my questions: 1. The image is on another server, can I just put location:http://domain.com/image.jpg? 2. How do I get it to download(save) the image, instead of just displaying it in the browser? Can I use readfile? Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/ Share on other sites More sharing options...
JustLikeIcarus Posted April 2, 2010 Share Posted April 2, 2010 Here maybe these headers will point you in the right direction header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/$mimetype"); header("Content-Transfer-Encoding: binary"); Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1035991 Share on other sites More sharing options...
phpnewbie8 Posted April 2, 2010 Share Posted April 2, 2010 I need some help with header(). I am trying to make it so an image will be downloaded when the user loads the php file. I can figure out header("location:image.jpg") but here are my questions: 1. The image is on another server, can I just put location:http://domain.com/image.jpg? 2. How do I get it to download(save) the image, instead of just displaying it in the browser? Can I use readfile? This is probably not the most complete example but it works: <?php header('Content-type: image/jpeg'); header('Content-Disposition: attachment; filename="image.jpg"'); readfile('http://middlezonemusings.com/wp-content/uploads/2007/03/eiffel-tower.jpg'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1035994 Share on other sites More sharing options...
jamiet757 Posted April 6, 2010 Author Share Posted April 6, 2010 I need some help with header(). I am trying to make it so an image will be downloaded when the user loads the php file. I can figure out header("location:image.jpg") but here are my questions: 1. The image is on another server, can I just put location:http://domain.com/image.jpg? 2. How do I get it to download(save) the image, instead of just displaying it in the browser? Can I use readfile? This is probably not the most complete example but it works: <?php header('Content-type: image/jpeg'); header('Content-Disposition: attachment; filename="image.jpg"'); readfile('[url=http://middlezonemusings.com/wp-content/uploads/2007/03/eiffel-tower.jpg%27]http://middlezonemusings.com/wp-content/uploads/2007/03/eiffel-tower.jpg'[/url]); ?> I tried that and it does download a file called image.jpg, the problem is it is corrupt, it does not display as an image, and the size does not correspond to the image it is supposed to be downloading. Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1037981 Share on other sites More sharing options...
jamiet757 Posted April 7, 2010 Author Share Posted April 7, 2010 Can anyone help me figure out how to make it read an external file from a URL? Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1038302 Share on other sites More sharing options...
JustLikeIcarus Posted April 7, 2010 Share Posted April 7, 2010 How about this? header('Content-type: image/jpeg'); header('Content-Disposition: attachment; filename="image.jpg"'); $content = file_get_contents('http://middlezonemusings.com/wp-content/uploads/2007/03/eiffel-tower.jpg'); echo $content; Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1038315 Share on other sites More sharing options...
jamiet757 Posted April 7, 2010 Author Share Posted April 7, 2010 Well I tried that and this time it actually downloaded the image (with the correct size) but it still didn't display as an image. Is there a way to keep the original filename instead of calling it image.jpg or whatever? Do I just remove the header('Content-Disposition: attachment; filename="image.jpg"'); line? Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1038341 Share on other sites More sharing options...
jamiet757 Posted April 7, 2010 Author Share Posted April 7, 2010 Ok, so here is what I have now: header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header('Content-type: image/jpeg'); header("Content-Transfer-Encoding: binary"); readfile($content); It downloads the file with the original filename (what I want) and when I open the image in Photoshop it displays correctly, however if I try to preview it using Windows Photo Viewer, it doesn't show up. This is for a stock image site, so when a customer downloads the image, it needs to work right otherwise I will get a lot of complaints. Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1038347 Share on other sites More sharing options...
JustLikeIcarus Posted April 7, 2010 Share Posted April 7, 2010 Ok I got this working fine for me. It wont give you the filename you want but that is easy to pull from the url. $file = 'http://middlezonemusings.com/wp-content/uploads/2007/03/eiffel-tower.jpg'; header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header('Content-type: image/jpeg'); header("Content-Transfer-Encoding: binary"); readfile($file); Quote Link to comment https://forums.phpfreaks.com/topic/197380-help-with-header/#findComment-1038361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.