Page 1 of 1

Automatically download image from another server

Posted: Sun Dec 23, 2007 3:58 pm
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.

Posted: Sun Dec 23, 2007 4:04 pm
by John Cartwright

Code: Select all

header("Content-Disposition: attachment; filename=\"$filename\"");
I believe you need to quotes around the filename

Posted: Sun Dec 23, 2007 4:11 pm
by jonwondering
Didn't work :(

Posted: Sun Dec 23, 2007 4:46 pm
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.

Posted: Sun Dec 23, 2007 4:52 pm
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!

Posted: Sun Dec 23, 2007 10:42 pm
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.

Posted: Sun Dec 23, 2007 10:44 pm
by John Cartwright
The problem I have with that is it won't force download, instead the browser may choose to handle the media.

Posted: Sun Dec 23, 2007 11:00 pm
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');

Posted: Sun Dec 23, 2007 11:46 pm
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? :)