|
|
|
date: Mon, 18 May 2009 23:55:18 +0100,
group: uk.net.web.authoring
back
File Download using PHP
Hi,
I'm trying to write a system that allows files to be downloaded from a
document server if the user is authorised.
I've seen
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
on the PHP website (http://uk2.php.net/function.readfile) which should
(once I wrap it in the right security stuff) do what I need.
However it does not seem to work in all browsers. My Firefox at home works
fine but using IE at work it simply outputs the binary of the PDF in ASCII
format on the page without any option to save or launch Acrobat. Any ideas
what I need to do to make this work across all browsers?
Thanks,
--
Graham Drabble
http://www.drabble.me.uk/
date: Mon, 18 May 2009 23:55:18 +0100
author: Graham Drabble
|
Re: File Download using PHP
Message-ID:
from
Graham Drabble contained the following:
>However it does not seem to work in all browsers. My Firefox at home works
>fine but using IE at work it simply outputs the binary of the PDF in ASCII
>format on the page without any option to save or launch Acrobat. Any ideas
>what I need to do to make this work across all browsers?
Some possible answers here
http://bytes.com/groups/net-asp/654415-response-contenttype-application-pdf
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
date: Tue, 19 May 2009 08:41:06 +0100
author: Geoff Berrow
|
Re: File Download using PHP
In message
, Graham
Drabble writes
>Hi,
>
>I'm trying to write a system that allows files to be downloaded from a
>document server if the user is authorised.
>
>I've seen
>
><?php
>$file = 'monkey.gif';
>
>if (file_exists($file)) {
> header('Content-Description: File Transfer');
> header('Content-Type: application/octet-stream');
> header('Content-Disposition: attachment; filename='.basename($file));
> header('Content-Transfer-Encoding: binary');
> header('Expires: 0');
> header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
> header('Pragma: public');
> header('Content-Length: ' . filesize($file));
> ob_clean();
> flush();
> readfile($file);
> exit;
>}
>?>
>
>on the PHP website (http://uk2.php.net/function.readfile) which should
>(once I wrap it in the right security stuff) do what I need.
>
>However it does not seem to work in all browsers. My Firefox at home works
>fine but using IE at work it simply outputs the binary of the PDF in ASCII
>format on the page without any option to save or launch Acrobat. Any ideas
>what I need to do to make this work across all browsers?
I think it is the Content-Type. I use the following code to serve PDFs
to be opened in the user's browser:
header ("HTTP/1.0 200 OK");
header('Content-type: application/pdf');
header ("Content-Disposition: inline; filename=$fname");
readfile($filename);
$fname is the name of the file excluding the path
$filename is the name of the file including the path
To get it to download instead of open in the browser you just need to
change the Content-Disposition to attachment
i.e.
header ("Content-Disposition: attachment; filename=$fname");
--
Dominic Sexton
date: Tue, 19 May 2009 10:01:07 +0100
author: Dominic Sexton {da-sep03}@dscs.demon.co.uk
|
Re: File Download using PHP
On Mon, 18 May 2009, Graham Drabble wrote:
> $file = 'monkey.gif';
> header('Content-Type: application/octet-stream');
Application/octet-stream is always wrong.
Use the correct MIME type, i.e. image/gif.
--
In memoriam Alan J. Flavell
http://groups.google.co.uk/groups/search?q=author:Alan.J.Flavell
date: Tue, 19 May 2009 17:53:05 +0200
author: Andreas Prilop
|
Re: File Download using PHP
On 19 May 2009 Andreas Prilop wrote in
news:Pine.LNX.4.64.0905191749310.12452@sarge.rrzn.uni-hannover.de:
> On Mon, 18 May 2009, Graham Drabble wrote:
>
>> $file = 'monkey.gif';
>> header('Content-Type: application/octet-stream');
>
> Application/octet-stream is always wrong.
> Use the correct MIME type, i.e. image/gif.
Thanks though interestingly the way I did it works on some other IE
browsers at work.
I'll try and make some form of lookup table of extension to MIME type,
problem is I don't know what files will be uploaded so need something
as a default.
If it's that wrong why is it in the PHP docs?
--
Graham Drabble
http://www.drabble.me.uk/
date: Tue, 19 May 2009 21:53:32 +0100
author: Graham Drabble
|
Re: File Download using PHP
On Tue, 19 May 2009 21:53:32 +0100, Graham Drabble
wrote in
:
>If it's that wrong why is it in the PHP docs?
Good question, but since PHP is not really the authoritative source for
information about Internet related things, lets go to IETF.
According to http://www.ietf.org/rfc/rfc2046.txt section 4.5.1.
"Octet-Stream Subtype":
The "octet-stream" subtype is used to indicate that a body contains
arbitrary binary data.
[...]
The recommended action for an implementation that receives an
"application/octet-stream" entity is to simply offer to put the data
in a file, with any Content-Transfer-Encoding undone, or perhaps to
use it as input to a user-specified process.
Unfortunately some widely used web software seems to have been written
by people who ignore the recommendations of the Internet Engineering
Task Force and the World Wide Web Consortium (the nearest we have to
standards bodies for web related things).
--
Owen Rees
[one of] my preferred email address[es] and more stuff can be
found at <http://www.users.waitrose.com/~owenrees/index.html>
date: Tue, 19 May 2009 23:49:42 +0100
author: Owen Rees
|
|
|