Jump to content

Help with header


jamiet757

Recommended Posts

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?

Link to comment
Share on other sites

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");

 

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.