Re: Generating "download" pages [message #186424 is a reply to message #186409] |
Wed, 23 July 2014 00:38 |
Curtis Dyer
Messages: 34 Registered: January 2011
Karma:
|
Member |
|
|
Lew Pitcher 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:
<snip>
>> 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.
I think you can make this much easier on yourself if you separate
the PHP code that generates the downloadable content into its own
file, and then have another resource that includes the HTML markup
and/or scripting that requests the download.
> 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.
Try something like the following model:
Make a PHP script that simply generates the downloadable content.
A very simple version might look like:
<?php
/* download.php */
define('FILENAME', 'img-name.png');
function outputfile($file = '')
{
$disposition = 'Content-Disposition: attachment';
if ($file)
$disposition .= "; filename=\"$file\"";
header('Content-Type: application/octet-stream');
header($disposition);
readfile(FILENAME);
}
/* output */
outputfile('foo.png');
?>
In a separate file, the "updated" page you want to display will
make the request for the download. You might also use the meta
refresh technique instead of client-side scripting.
<?php
/* update_page.php */
/*
* stuff ...
*/
?><!DOCTYPE html>
<html lang="en">
<head><title>Hello, world</title></head>
<body>
<h1>Users will see this</h1>
<p>
If they don't get the prompt, the user can
<a href="download.php">click here</a>.
</p>
<script>
window.location = "http://example.com/download.php";
</script>
</body>
</html>
With regard to putting these files to use, you might have
"update_page.php" the target of a form action, or something to
that effect (as Tim Streater provided). From there, you could pass
on any data to "download.php" itself in order to dynamically
generate the needed content.
<snip>
--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
|
|
|