Re: Generating "download" pages [message #186410 is a reply to message #186409] |
Sat, 19 July 2014 18:14 |
Lew Pitcher
Messages: 60 Registered: April 2013
Karma:
|
Member |
|
|
On Saturday 19 July 2014 13:53, in comp.lang.php, "Lew Pitcher"
<lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote:
> 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.
Here's the PHP I used for this test:
<?php
function SendTheFile()
{
$file="./SixSpot_d.png";
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));
readfile($file);
}
session_start();
switch ($_SERVER['REQUEST_METHOD'])
{
case 'GET':
$count = 0;
break;
case 'POST':
$count = $_SESSION['Count'];
if (isset($_POST['Action']) && $_POST['Action'] == 'Send')
SendTheFile();
$count = $count + 1;
break;
}
?>
<html>
<head><title>Trial Sendfile</title></head>
<body>
<h1><?php print $count ?></h1>
<p>Did you get it?</p>
<form method="post">
<input type="submit" name="Action" value="Send"/>
</form>
</body>
</html>
<?php
$_SESSION['Count'] = $count;
?>
Note that the named file ("./SixSpot_d.png") is a PNG graphic, residing in
the same directory as this PHP page. I chose this file arbitrarily; if the
PHP does what I want, I should see
a) On initial entry, the web page displays a header of "O" and the Send
button
b) When the "Send" button is selected, the web page should refresh to
show "1" as the page header (rather than "0"), and I should receive
the SixSpot_d.png in my download directory.
What I actually see is
a) On initial entry, the web page displays a header of "O" and the Send
button
b) When the "Send" button is selected, the browser does download the
selected file, but does not refresh the webpage.
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
|
|
|