Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Downloading a GIF

Posted on 2003-03-31
27
Medium Priority
?
388 Views
Last Modified: 2008-02-01
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.

__________________

0
Comment
Question by:rahulsjadhav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
  • +3
27 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8243636
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
 
LVL 15

Expert Comment

by:VGR
ID: 8243639
sorry :

  set_magic_quotes_runtime(0);
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 8243787
try this:
header("Content-disposition: attachment; filename = image.gif");
header("Content-Type: image/gif");
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rahulsjadhav
ID: 8244020
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244263
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
 

Author Comment

by:rahulsjadhav
ID: 8244305
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244344
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
 

Author Comment

by:rahulsjadhav
ID: 8244357
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
 

Author Comment

by:rahulsjadhav
ID: 8244421
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244430
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244437
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
 

Author Comment

by:rahulsjadhav
ID: 8244499
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244507
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244526
as for the email, if you want :
vgraux at easynet.be
0
 
LVL 15

Expert Comment

by:VGR
ID: 8244527
send me the original, too
0
 

Author Comment

by:rahulsjadhav
ID: 8244530
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
 

Author Comment

by:rahulsjadhav
ID: 8244572
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
 
LVL 15

Expert Comment

by:VGR
ID: 8244608
never click "reload" on EE.

Click "reload question" (left menu, first link under your pseudonym)
0
 
LVL 15

Expert Comment

by:VGR
ID: 8245855
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
 

Author Comment

by:rahulsjadhav
ID: 8258778
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
 
LVL 15

Expert Comment

by:VGR
ID: 8279499
what's up since I sent you the explanation ?
0
 

Author Comment

by:rahulsjadhav
ID: 8281553
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
 
LVL 15

Expert Comment

by:VGR
ID: 8282008
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
 

Author Comment

by:rahulsjadhav
ID: 8282034
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
 

Expert Comment

by:hitting_30_hard
ID: 8659342
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
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11942921
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
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 11981155
PAQed, with points refunded (75)

Netminder
Site Admin
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question