PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
jonwondering
Forum Commoner
Posts: 39 Joined: Mon Mar 13, 2006 6:26 pm
Post
by jonwondering » Sun Dec 23, 2007 3:58 pm
I am trying to give the user an option of downloading an image (by clicking on a link, instead of "Right-click > Save as...") from another server by changing the headers like this:
Code: Select all
$filename = 'http://www.anotherserver.com/image.jpg';
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen($filename, "r");
print fread($fp, filesize($filename));
fclose($fp);
exit;
It seems the image is being downloaded because it's size is correct, but it's name is whatever $filename contains (the whole link), and the image cannot be opened since it is corrupted. What am I doing wrong? Any ideas?
Thanks.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Dec 23, 2007 4:04 pm
Code: Select all
header("Content-Disposition: attachment; filename=\"$filename\"");
I believe you need to quotes around the filename
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Dec 23, 2007 4:46 pm
Code: Select all
header("Content-Disposition: attachment; filename=\"". basename($filename)."\"");
Hrmm, been awhile. I think you only need to pass the basename of the file and not the filepath.
Probably should be using
readfile() instead of
fopen() also, although I know for sure they have some examples of forced downloading there.
jonwondering
Forum Commoner
Posts: 39 Joined: Mon Mar 13, 2006 6:26 pm
Post
by jonwondering » Sun Dec 23, 2007 4:52 pm
Ah, you are awesome. It works. Here's the final version:
Code: Select all
$filename = 'http://www.anotherserver.com/image.jpg';
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);
Thanks Jcart!
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Sun Dec 23, 2007 10:42 pm
wouldn't a redirect header be more efficient here? If I'm thinking straight it would eliminate 2/3 of the traffic. Maybe browsers don't deal well with redirected images, but I don't see why not.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Dec 23, 2007 10:44 pm
The problem I have with that is it won't force download, instead the browser may choose to handle the media.
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Sun Dec 23, 2007 11:00 pm
hmmm... works in FF on Linux - anyone have windows handy?
Page one:
Page two:
Code: Select all
header('location:http://some.other.server/image.jpg');
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Dec 23, 2007 11:46 pm
Kieran Huggins wrote: hmmm... works in FF on Linux - anyone have windows handy?
Page one:
Page two:
Code: Select all
header('location:http://some.other.server/image.jpg');
Didn't work on my Firefox 2.0 on Ubuntu
Didn't work on IE 5.0, 5.5, or 6.0 (yes I have all 3 versions installed)
And who needs Windows when you have Wine?