Re: Generating "download" pages [message #186409 is a reply to message #186406] |
Sat, 19 July 2014 17:53 |
Lew Pitcher
Messages: 60 Registered: April 2013
Karma:
|
Member |
|
|
On Saturday 19 July 2014 12:36, in comp.lang.php, "Tim Streater"
<timstreater(at)greenbee(dot)net> wrote:
> In article <b_wyv.50770$OC3(dot)15102(at)fx18(dot)iad>, Lew Pitcher
> <lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote:
>
>> Research has shown conflicting advice wrt the necessary headers, the
>> order of the headers, the presence or absence of the web page HTML, the
>> PHP functions required to send the file, etc.
>
> Here's what I do to send a file:
>
> <?php
>
> $file = '/path/to/somefile.zip';
>
> 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);
>
> ?>
>
> and this works for me. The file containing this PHP is the action of a
> <form> which is submitted when the user clicks a button.
>
Thanks, Tim, but this doesn't work for me, and seems to have problems in
general.
Instead of getting a refreshed web page (containing new content, as per the
PHP processing of the $_POST data) /and/ a (in this case PNG) file
downloading, I get a display of the PNG file (as if it had been imbedded in
it's own webpage) and nothing else.
A critique (elsewhere) of this sort of code points out
* Content-Description is not a valid HTTP header
(not mentioned at all in RFC 2616)
* Content-Type should be set to the actual media type, or none at all.
* The code for Content-Disposition will produce incorrect headers for many
filenames.
* Content-Transfer-Encoding is not a valid HTTP header (explicitly specified
in RFC 2616 pp 19.4.5 "No Content-Transfer-Encoding")
I realize that this code comes almost directly from PHP.NET, being
documented in the "manual" page for readfile(). But, that doesn't mean that
it is correct, or even that it works.
Sorry, but it looks like I have to keep trying.
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
|
|
|