Downloading a GIF

The GIF is there on the hard disk, which is under control of my application. There is a button on click of which this file can be downloaded on the harddisk anywhere.

I am setting following properties
$fp = fopen($filename,"r");
header("Content-disposition: attachment; filename");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
readfile("$filename");

with this the download dialog opens and GIF file gets downloaded. But when the GIF file is opened in browser then it is not seen.

When opened in browser, it shows a Broken Image. It is happening with all the GIF file.

__________________

rahulsjadhavAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NetminderConnect With a Mentor Commented:
PAQed, with points refunded (75)

Netminder
Site Admin
0
 
VGRCommented:
set_magic_quote(0);

before readfile() call


BTW, I'm not sure this is correct
header("Content-disposition: attachment; filename");
as there is no filename specified
0
 
VGRCommented:
sorry :

  set_magic_quotes_runtime(0);
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
lexxwernCommented:
try this:
header("Content-disposition: attachment; filename = image.gif");
header("Content-Type: image/gif");
0
 
rahulsjadhavAuthor Commented:
I tried set_magic_quotes_runtime(0);      but still same problem.

The "filename" contains actual filename when code runs.

Also
header("Content-disposition: attachment; filename=image.gif");
header("Content-Type: image/gif");

Is not making any difference.
0
 
VGRCommented:
try just this :

<?
$filename='image.gif'; // or whatever you have
header("Content-disposition: attachment; filename = $filename");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile("$filename");
exit;
?>

I just tested it, it works PERFECTLY and I've the good image at the end.
0
 
rahulsjadhavAuthor Commented:
VGR

Pl try opening the downloaded file.

I am getting the file downloaded.But when I doubleclick and open the gif file in browser , then a broken image symbol is seen. Thats the problem.

My browser is IE 5.5 SP2.
0
 
VGRCommented:
I told you already : I've a perfect image at the end. I tried with a gif

do you want to see a screen shot "proof" ? :D

My config : IE 5.0.3315, Win2K SP3, PHP 4.2.3 std modules, std config
0
 
rahulsjadhavAuthor Commented:
VGR

Pl try opening the downloaded file.

I am getting the file downloaded.But when I doubleclick and open the gif file in browser , then a broken image symbol is seen. Thats the problem.

My browser is IE 5.5 SP2.
0
 
rahulsjadhavAuthor Commented:
VGR

I do not deny if you did get the gif opened properly. :)
I am worried why the similar code is not working at my end.
following is the exact code I am using.

$file="more_blue.gif";
header("Content-disposition: attachment; filename=$file");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile("$filename");
die;

The GIF is downloaded but unfortunately I am not able to open and view it. It shows a broken Image symbol.

PHP version is 4.3
0
 
VGRCommented:
excuse-me, but perhaps die() stops the output buffering ?!?
whi not exit; or nothing (if end -of-script) ?

are you on *nix ?

is the image file readable by apache (httpd or apache user)
0
 
VGRCommented:
and FYI here's the exact code I used (placed in the same directory as the image files)

<?
$filename='image2.gif'; // or whatever you have
header("Content-disposition: attachment; filename = $filename");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile("$filename");
exit;
?>
0
 
rahulsjadhavAuthor Commented:
VGR

I tried exit; in place of die; but there is no change.

I also tried downloading the image in the same folder.

I think the properties of the GIF are eaten. As when I try to open the file in any Image opener "Image Preview" OR "GIF Works" then it shows format not supported.

But also when I move cursor near the downloaded fie it shows GIF.

Unfortunately I am not able to attach the Image here, so that you could have a lok at the file. Possibly I could E mail you the file, If I can know your E Mail address.


0
 
VGRCommented:
just to know : the ORIGINAL file is ok ?

let's add the set_magic_quotes(0) stuff this way :

<?
$filename='image2.gif'; // or whatever you have
header("Content-disposition: attachment; filename = $filename");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
set_magic_quotes_runtime(0);
readfile("$filename");
flush();
exit;
?>
0
 
VGRCommented:
as for the email, if you want :
vgraux at easynet.be
0
 
VGRCommented:
send me the original, too
0
 
rahulsjadhavAuthor Commented:
VGR

I tried exit; in place of die; but there is no change.

I also tried downloading the image in the same folder.

I think the properties of the GIF are eaten. As when I try to open the file in any Image opener "Image Preview" OR "GIF Works" then it shows format not supported.

But also when I move cursor near the downloaded fie it shows GIF.

Unfortunately I am not able to attach the Image here, so that you could have a lok at the file. Possibly I could E mail you the file, If I can know your E Mail address.


0
 
rahulsjadhavAuthor Commented:
VGR

I tried exit; in place of die; but there is no change.

I also tried downloading the image in the same folder.

I think the properties of the GIF are eaten. As when I try to open the file in any Image opener "Image Preview" OR "GIF Works" then it shows format not supported.

But also when I move cursor near the downloaded fie it shows GIF.

Unfortunately I am not able to attach the Image here, so that you could have a lok at the file. Possibly I could E mail you the file, If I can know your E Mail address.


0
 
VGRCommented:
never click "reload" on EE.

Click "reload question" (left menu, first link under your pseudonym)
0
 
VGRCommented:
ok, who has an idea why the CSS reference ends up in the binary data ???!?? :D

(a prepend-ed file ?)

<img src="http://www.fecj.org/extra/wonderful.jpg">
0
 
rahulsjadhavAuthor Commented:
VGR

I tried exit; in place of die; but there is no change.

I also tried downloading the image in the same folder.

I think the properties of the GIF are eaten. As when I try to open the file in any Image opener "Image Preview" OR "GIF Works" then it shows format not supported.

But also when I move cursor near the downloaded fie it shows GIF.

Unfortunately I am not able to attach the Image here, so that you could have a lok at the file. Possibly I could E mail you the file, If I can know your E Mail address.


0
 
VGRCommented:
what's up since I sent you the explanation ?
0
 
rahulsjadhavAuthor Commented:
Yes I found out why the non required tag was added in the file.
I added ob_end_clean to erase all output buffer, before starting output for file downloading. This removed the problem.

Thanks again for your observation all the kind help.
0
 
VGRCommented:
cool. You should hve said you were using the ob_* functions 8-)

The cause would have been obvious to me and to everyone here 8-))
0
 
rahulsjadhavAuthor Commented:
Thanks Vincent

I will keep asking you the doubts, if not through this experts exchange then through the mail. I hope mailing you will not trouble you.



0
 
hitting_30_hardCommented:
I've recently had the same sort or problem and have figured out some stuff I will post here the knowledge I have and hopefully someone will find the proper answer.

my problem is that IE 6 would not open the file after downloading it but I can save to desktop and open with no problems
files are stored in unique names and the download returns the file as the original filename.


I am testing with PDF files adobe reader is saying that the file is not found

header("Content-Disposition: attachment; filename=".basename($f_name).";");
forces the browser to ask where you want to save the file selecting open return error but will save correctly

header("Content-Disposition: filename=".basename($f_name).";");
will open the file in the browser no problem.

My reasoning is that in setting the content disposition filename to the original is causing the problem when forcing download. ie is loosing the filename as app loads

server sends file abc.md5 with rename of xyz.pdf
IE launches adobe app but is passing abc.md5 instead of xyz.pdf

resaoning comes from testing with a word document selecting option open
word app loads and fails to open doc, error msg reads "file not found cghdt[1]/test.doc" (temp internet folder)
don't close word and click link opens perfectly



-- code sample --
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Type: $f_mime");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($f_name).";");

/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

$fp = fopen("$filename","rb");
  fpassthru($fp);
-- end code --
0
 
snoyes_jwCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    PAQ with points refunded

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

snoyes_jw
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.