Re: [PHP] Re: Setting headers for file download

2006-06-14 Thread Frank Arensmeier
I was working on a script that was supposed to generate PDF files on  
the fly and ran into the same problem as you described. MS IE has  
always been and will always be (?) a P.I.T.A. when it comes to  
following standards (I am not really sure if the header application/x- 
octet-steam is a standrad in order to force a download). I came up  
with this little helper:


?php
$filename = example.pdf;
$len = filesize ($filename );

if ( ( preg_match('|MSIE ([0-9.]+)|', $agent, $version ) ) ||  
( preg_match( '|Internet Explorer/([0-9.]+)|', $agent, $version ) ) )

{
header( 'Content-Type: application/x-msdownload' );
header( 'Content-Length: ' . $len );
if ( $version == '5.5' )
{
header( 'Content-Disposition: filename=' . $filename . '' );
} else {
		header( 'Content-Disposition: attachment; filename=' . $filename .  
'' );

}
} else {
header( 'Content-Type: application/pdf' );
header( 'Content-Length: ' . $len );
header( 'Content-disposition: attachment; filename=' . $filename );
}
echo file_get_contents($filename);
?

All browsers I have tested with do show the download frame (Opera,  
Safari, IE, Firefox, Mozilla browsers). But, as Richard already  
mentioned, I had to fake the download URL, like http:// 
www.site.com/downloads/example.pdf. In the folder downloads I put  
a .htaccess file with the line ErrorDocument 404 /link/to/my/php/ 
script.php.


/frank

14 jun 2006 kl. 02.19 skrev Peter Lauri:


I will try that after golf...

-Original Message-
From: Rafael [mailto:[EMAIL PROTECTED]
Sent: Wednesday, June 14, 2006 6:28 AM
To: php-general@lists.php.net
Subject: [PHP] Re: Setting headers for file download

I use something like this...
   $file_len = filesize($file_name);
   $file_ts  = filemtime($file_name);
   header('Content-Type: application/x-octet-stream'); // none known
   header('Cache-Control: must-revalidate, post-check=0, pre- 
check=0');
   header('Last-Modified: '. gmdate('D, d M Y H:i:s', $file_ts) .'  
GMT');

   header('Content-Disposition: attachment; filename='.
basename($file_name) .'');
   header(Content-Length: $file_len);
   readfile($file_name);


Peter Lauri wrote:

Best group member,

This is how I try to push files to download using headers:

header(Content-type: $file_type);
header(Content-Disposition: attachment; filename=$filename);
print $file;

It works fine in FireFox, but not that good in IE. I have been  
googled to

find the fix for IE, but I can not find it. Anyone with experience of

this?


Best regards,
Peter Lauri


--
Atentamente / Sincerely,
J. Rafael Salazar Magaña

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Re: Setting headers for file download

2006-06-14 Thread Peter Lauri
I got it working on one server, but not the real server. I just took your
code and voila it was working (with some modifications).

It works on:
Localhost Windows
Plesk Server running Plesk 7.5

Not working on:
Plesk Server running Plesk 7.0

Then the questions is: Where should I start to search in WHY the server is
not sending the correct headers? This is becoming more and more irritating
:)

Best regards,
Peter Lauri



-Original Message-
From: Rafael [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, June 14, 2006 6:28 AM
To: php-general@lists.php.net
Subject: [PHP] Re: Setting headers for file download

I use something like this...
   $file_len = filesize($file_name);
   $file_ts  = filemtime($file_name);
   header('Content-Type: application/x-octet-stream'); // none known
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Last-Modified: '. gmdate('D, d M Y H:i:s', $file_ts) .' GMT');
   header('Content-Disposition: attachment; filename='. 
basename($file_name) .'');
   header(Content-Length: $file_len);
   readfile($file_name);


Peter Lauri wrote:
 Best group member,
 
 This is how I try to push files to download using headers:
 
 header(Content-type: $file_type);
 header(Content-Disposition: attachment; filename=$filename); 
 print $file;
 
 It works fine in FireFox, but not that good in IE. I have been googled to
 find the fix for IE, but I can not find it. Anyone with experience of
this?
 
 Best regards,
 Peter Lauri

-- 
Atentamente / Sincerely,
J. Rafael Salazar Magaña

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Re: Setting headers for file download

2006-06-14 Thread Peter Lauri
That did it, thanks...

Ok, my knowledge about HTTP is not the best. But how can you send three
different content-type headers? :)


-Original Message-
From: Barry [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, June 14, 2006 8:36 PM
To: php-general@lists.php.net
Subject: [PHP] Re: Setting headers for file download

Peter Lauri schrieb:
 Best group member,
 
 This is how I try to push files to download using headers:
 
 header(Content-type: $file_type);
 header(Content-Disposition: attachment; filename=$filename); 
 print $file;
 
 It works fine in FireFox, but not that good in IE. I have been googled to
 find the fix for IE, but I can not find it. Anyone with experience of
this?
 
 Best regards,
 Peter Lauri

There ya go:
// fix for IE catching or PHP bug issue
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);

 // use the Content-Disposition header to supply a recommended 
filename and
 // force the browser to display the save dialog.
 header(Content-Disposition: attachment; filename=.$path.;);

 header(Content-Transfer-Encoding: binary);

 header(Content-Length: .filesize($file));

 readfile($file);

-- 
Smileys rule (cX.x)C --o(^_^o)
Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Setting headers for file download

2006-06-14 Thread Barry

Peter Lauri schrieb:

That did it, thanks...

Ok, my knowledge about HTTP is not the best. But how can you send three
different content-type headers? :)



There are not so different at all.
Just giving the browser the job to download that thing.

Every browser likes to interpret every content-type like he wants to.
They ignore mostly every type they dislike.

That's the problem you encounter in using firefox/IE.

Greets
Barry

--
Smileys rule (cX.x)C --o(^_^o)
Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Setting headers for file download [medium]

2006-06-14 Thread Rafael

Barry wrote:

Peter Lauri schrieb:

Ok, my knowledge about HTTP is not the best. But how can you send three
different content-type headers? :)


There are not so different at all.
Just giving the browser the job to download that thing.

Every browser likes to interpret every content-type like he wants to.
They ignore mostly every type they dislike.


	According to the manual, header() will replace previous similar 
headers with the new one sent, which would be

  header(Content-Type: application/download);

The optional replace parameter indicates whether the header should 
replace a previous similar header, or add a second header of the same 
type. By default it will replace, but if you pass in FALSE as the second 
argument you can force multiple headers of the same type

[1]http://php.net/header

	As for the value of Content-Type it doesn't matter, you only have to 
specify an unknown (MIME) type so the browser won't know what to do with 
it, hence forcing to download the file.


	As minor notes (after reading a bit the RFC)... Expires is expected 
to be a date, not a number, and Pragma value conflicts with 
Cache-control, since...

   public
  Indicates that the response MAY be cached by any cache, even if it
  would normally be non-cacheable or cacheable only within a non-
  shared cache. (See also Authorization, section 14.8, for
  additional details.)

	Also, it seems we're given a wrong use for must-revalidate (me 
included), that or I misunderstood the RFC documentation --I didn't even 
found pre-check  post-check.  Content-Disposition is not part of 
the HTTP standard and Content-Transfer-Encoding is ignored, since both 
seem to be for e-mail usage (LOL) --and it's possible values are 
quoted-printable o base64--.  Finally, Content-Length and 
Content-Type seem to apply only if had the request been a GET.


	Interesting what you learn reading the RFCs, too bad they're too long 
and sometimes not that clear.

RFC: http://www.faqs.org/rfcs/rfc2616
--
Atentamente / Sincerely,
J. Rafael Salazar Magaña

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Re: Setting headers for file download

2006-06-13 Thread Peter Lauri
I will try that after golf...

-Original Message-
From: Rafael [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, June 14, 2006 6:28 AM
To: php-general@lists.php.net
Subject: [PHP] Re: Setting headers for file download

I use something like this...
   $file_len = filesize($file_name);
   $file_ts  = filemtime($file_name);
   header('Content-Type: application/x-octet-stream'); // none known
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Last-Modified: '. gmdate('D, d M Y H:i:s', $file_ts) .' GMT');
   header('Content-Disposition: attachment; filename='. 
basename($file_name) .'');
   header(Content-Length: $file_len);
   readfile($file_name);


Peter Lauri wrote:
 Best group member,
 
 This is how I try to push files to download using headers:
 
 header(Content-type: $file_type);
 header(Content-Disposition: attachment; filename=$filename); 
 print $file;
 
 It works fine in FireFox, but not that good in IE. I have been googled to
 find the fix for IE, but I can not find it. Anyone with experience of
this?
 
 Best regards,
 Peter Lauri

-- 
Atentamente / Sincerely,
J. Rafael Salazar Magaña

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php