Automatically download image from another server

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

Post Reply
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Automatically download image from another server

Post by jonwondering »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

header("Content-Disposition: attachment; filename=\"$filename\"");
I believe you need to quotes around the filename
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post by jonwondering »

Didn't work :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

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 »

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!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The problem I have with that is it won't force download, instead the browser may choose to handle the media.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

hmmm... works in FF on Linux - anyone have windows handy?

Page one:

Code: Select all

<img src="page_2.php"/>
Page two:

Code: Select all

header('location:http://some.other.server/image.jpg');
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Kieran Huggins wrote:hmmm... works in FF on Linux - anyone have windows handy?

Page one:

Code: Select all

<img src="page_2.php"/>
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? :)
Post Reply