Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14108 > unrolled thread
| Started by | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| First post | 2014-07-18 09:55 -0400 |
| Last post | 2014-07-23 00:38 +0000 |
| Articles | 6 on this page of 26 — 7 participants |
Back to article view | Back to comp.lang.php
Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-18 09:55 -0400
Re: Generating "download" pages Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-18 17:39 +0200
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 11:38 -0400
Re: Generating "download" pages Jerry Stuckle <jstucklex@attglobal.net> - 2014-07-19 16:57 -0400
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 17:06 -0400
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 11:58 -0400
Re: Generating "download" pages Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-19 19:06 +0200
Re: Generating "download" pages "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-07-19 19:43 +0200
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 14:23 -0400
Re: Generating "download" pages "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-07-19 22:18 +0200
Re: Generating "download" pages "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-07-19 22:38 +0200
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 16:54 -0400
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 17:00 -0400
Resolved: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-20 14:34 -0400
Re: Generating "download" pages Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-19 20:50 +0200
Re: Generating "download" pages "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-07-19 22:06 +0200
Re: Generating "download" pages Jerry Stuckle <jstucklex@attglobal.net> - 2014-07-19 16:54 -0400
Re: Generating "download" pages Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-20 00:48 +0200
Re: Generating "download" pages Arno Welzel <usenet@arnowelzel.de> - 2014-07-20 01:04 +0200
Re: Generating "download" pages Arno Welzel <usenet@arnowelzel.de> - 2014-07-19 18:17 +0200
Re: Generating "download" pages Jerry Stuckle <jstucklex@attglobal.net> - 2014-07-19 16:55 -0400
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 12:25 -0400
Re: Generating "download" pages Tim Streater <timstreater@greenbee.net> - 2014-07-19 17:36 +0100
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 13:53 -0400
Re: Generating "download" pages Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-07-19 14:14 -0400
Re: Generating "download" pages Curtis Dyer <dyer85@gmail.com> - 2014-07-23 00:38 +0000
Page 2 of 2 — ← Prev page 1 [2]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2014-07-19 16:55 -0400 |
| Message-ID | <lqem07$8m0$2@dont-email.me> |
| In reply to | #14113 |
On 7/19/2014 12:17 PM, Arno Welzel wrote:
> Lew Pitcher, 2014-07-18 15:55:
>
>> I'm not certain that what I want can be done, let alone done with PHP, but
>> please let me describe the function I'm looking for:
>>
>>
>> On response to a trigger on one web page, I want to be able to generate and
>> display a new web page, /and/ at the same time, send an associated file as
>> a download.
>>
>> The user selects (in this case) "Export recipes" (I'm developing a PHP/MySQL
>> recipe management application), which causes
>> 1) the page to change to a "Download in progress" page, and
>> 2) a file (in this case, an XML file containing the recipes) to be sent to
>> the client.
>>
>> Is this doable? How do I do this with PHP?
>
> I would solve it this way:
>
> A PHP script displays the page *and* also generates a
> META-Refresh-Header to redirect the browser to another script (or the
> same whith an additional parameter) which will then generate the content
> to be downloaded.
>
> As a rough example (untested and not complete - just to get the idea):
>
> <?php
> //
> // getfile.php
> //
>
> if(!isset($_GET['download'))
> {
> ?>
> <!DOCTYPE html>
> <html>
> <head>
> <meta
> http-equiv="refresh"
> content="5; url=http://example.com/getfile.php?download=1" />
> <title>Download</title>
> </head>
> <body>
> <p>If your download does not start automatically,
> <a href="http://example.com/getfile.php?download=1">click here</a>.</p>
> </body>
> </html>
> <?
> }
> else
> {
> header("Content-Disposition: attachment; filename=download.xml");
>
> // Add XML output to be downloaded here...
> }
> ?>
>
>
Arno,
An interesting idea. I admit I hadn't thought of using a meta refresh
that way; it's worth a look. The couple of times I've needed to do
something like this I've used javascript.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2014-07-19 12:25 -0400 |
| Message-ID | <b_wyv.50770$OC3.15102@fx18.iad> |
| In reply to | #14108 |
I will elaborate on the processing I'm trying to perform: On Friday 18 July 2014 09:55, in comp.lang.php, "Lew Pitcher" <lew.pitcher@digitalfreehold.ca> wrote: > I'm not certain that what I want can be done, let alone done with PHP, but > please let me describe the function I'm looking for: > > > On response to a trigger on one web page, I want to be able to generate > and display a new web page, /and/ at the same time, send an associated > file as a download. > > The user selects (in this case) "Export recipes" (I'm developing a > PHP/MySQL recipe management application), which causes > 1) the page to change to a "Download in progress" page, and > 2) a file (in this case, an XML file containing the recipes) to be sent to > the client. I already have a working PHP/MySQL/HTML page that permits the end user to refine a list of known recipes by various criteria. This same page provides options, once the user has adequately refined the recipe list, to View a single recipe, or to Export all the selected recipes. It is this Export function that I am now designing. Conceptually, if the user selects the "Export" option on the menu page, the browser would display an "Export Options" page that lists the various export formats the selected recipes can be saved as. Initially, this would include PDF (for offline viewing or printing), and as a recipe interchange file (possibly a MealMaster file, although I would like to include to a custom XML representation as well. On this "Export Options" page, the user will 1) select the appropriate option, and then 2) select the "Send" button The PHP behind this page will then a) build the appropriate transport format file b) send the file to the user, and c) resend the "Export Options" page. From the users POV, he a) sees the initial "Export Options" page, b) selects the requisite transport format for the selected recipes, c) selects the "Send" button, d) sees a redisplayed "Export Options" page, and e) sees a file (of appropriate type) downloading (or the browsers Download menu, if default download options have not been set) At this point, the user can then either select a new transport format, and go through the process again, or select a "Return to Menu" option that redirects to the menu PHP file. > Is this doable? How do I do this with PHP? 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. -- Lew Pitcher "In Skills, We Trust" PGP public key available upon request
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2014-07-19 17:36 +0100 |
| Message-ID | <190720141736307543%timstreater@greenbee.net> |
| In reply to | #14114 |
In article <b_wyv.50770$OC3.15102@fx18.iad>, Lew Pitcher
<lew.pitcher@digitalfreehold.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.
--
"The idea that Bill Gates has appeared like a knight in shining armour to
lead all customers out of a mire of technological chaos neatly ignores
the fact that it was he who, by peddling second-rate technology, led them
into it in the first place." - Douglas Adams
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2014-07-19 13:53 -0400 |
| Message-ID | <Pgyyv.37816$YC3.25040@fx04.iad> |
| In reply to | #14115 |
On Saturday 19 July 2014 12:36, in comp.lang.php, "Tim Streater"
<timstreater@greenbee.net> wrote:
> In article <b_wyv.50770$OC3.15102@fx18.iad>, Lew Pitcher
> <lew.pitcher@digitalfreehold.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
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2014-07-19 14:14 -0400 |
| Message-ID | <gAyyv.40675$C87.32799@fx07.iad> |
| In reply to | #14119 |
On Saturday 19 July 2014 13:53, in comp.lang.php, "Lew Pitcher"
<lew.pitcher@digitalfreehold.ca> wrote:
> On Saturday 19 July 2014 12:36, in comp.lang.php, "Tim Streater"
> <timstreater@greenbee.net> wrote:
>
>> In article <b_wyv.50770$OC3.15102@fx18.iad>, Lew Pitcher
>> <lew.pitcher@digitalfreehold.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
[toc] | [prev] | [next] | [standalone]
| From | Curtis Dyer <dyer85@gmail.com> |
|---|---|
| Date | 2014-07-23 00:38 +0000 |
| Message-ID | <lqn06j$a40$1@dont-email.me> |
| In reply to | #14119 |
Lew Pitcher wrote:
> On Saturday 19 July 2014 12:36, in comp.lang.php, "Tim Streater"
> <timstreater@greenbee.net> wrote:
>
>> In article <b_wyv.50770$OC3.15102@fx18.iad>, Lew Pitcher
>> <lew.pitcher@digitalfreehold.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);?>
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.php
csiph-web