Re: Re: Setting headers for file download

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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:rsalazarm@xxxxxxxxx]
Sent: Wednesday, June 14, 2006 6:28 AM
To: php-general@xxxxxxxxxxxxx
Subject:  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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux