Downloads, headers and scripts not finishing...

Downloads, headers and scripts not finishing...

am 20.10.2007 20:57:48 von Dahak

I may be having an attack of the stupids and my Google-foo is
failing me today, but I can't seem to get past this problem.


I have a script that indexes a directory and provides the user
with a simple interface to select files to download.

Once they've made their selection and submitted the form, the form
action reloads the page.

On reload, the script checks for the presence of POST variables
and, if not found, redisplays the previous form or, if found collects
files for download.

If this is the first request, the target files are collected into
a ZIP file and saved to the server for reuse.

Then the script serves the saved file to the user for download
with the following code:

---

header('Pragma: public');
header('Content-type: application/zip');
header('Content-length: ' . filesize($zipdir . '/' . $zipfilename));
header('Content-Disposition: attachment;
filename="'.$zipfilename.'"');

// readfile( $zipdir . '/' . $zipfilename );
if ( ( $fp = fopen( $zipdir . '/' . $zipfilename, 'rb' ) ) === false )
exit;
@fpassthru( $fp );
@fclose( $fp );

---

This all works fine. The commented out readfile() _also_ worked
fine, but I've been trying to see how to get past my problem, which
is:

Once the file is sent to the output stream, the script seems to
halt.

I want to get the script to continue rendering a 'Thank you for
downloading...'-type page, but nothing seems to happen after the
readfile() or fpassthru().

In practical terms, this is nothing more than an annoyance. The
end user gets the requested ZIP file, but nothing else can be done in
the script. (I have a 'busy' graphic that I unhide on form submission
that I want to hide again upon completion.)


What am I forgetting? Is there something in the header man pages
that I've missed?

Other examples I've searched for online seem to imply that script
execution should continue.


Any thoughts would be appreciated.

-Joe

Re: Downloads, headers and scripts not finishing...

am 20.10.2007 21:49:46 von Michael Fesser

..oO(Dahak)

> This all works fine. The commented out readfile() _also_ worked
>fine, but I've been trying to see how to get past my problem, which
>is:
>
> Once the file is sent to the output stream, the script seems to
>halt.

Correct, that's how it works.

> I want to get the script to continue rendering a 'Thank you for
>downloading...'-type page, but nothing seems to happen after the
>readfile() or fpassthru().

That would be an entirely different thing. The browser requests one
resource and gets one, a ZIP file in this case. For another resource
like an HTML page for example the browser would have to send another
request, because you can't send two resources back at the same. One
request, one response, one resource.

> In practical terms, this is nothing more than an annoyance. The
>end user gets the requested ZIP file, but nothing else can be done in
>the script.

It can't be done in the script because it can't be done in HTTP.

> Other examples I've searched for online seem to imply that script
>execution should continue.

Of course the script execution can continue, but you can't send a second
document back to the browser after the first requested file was sent.

Micha

Re: Downloads, headers and scripts not finishing...

am 20.10.2007 22:11:43 von Jerry Stuckle

Dahak wrote:
> I may be having an attack of the stupids and my Google-foo is
> failing me today, but I can't seem to get past this problem.
>
>
> I have a script that indexes a directory and provides the user
> with a simple interface to select files to download.
>
> Once they've made their selection and submitted the form, the form
> action reloads the page.
>
> On reload, the script checks for the presence of POST variables
> and, if not found, redisplays the previous form or, if found collects
> files for download.
>
> If this is the first request, the target files are collected into
> a ZIP file and saved to the server for reuse.
>
> Then the script serves the saved file to the user for download
> with the following code:
>
> ---
>
> header('Pragma: public');
> header('Content-type: application/zip');
> header('Content-length: ' . filesize($zipdir . '/' . $zipfilename));
> header('Content-Disposition: attachment;
> filename="'.$zipfilename.'"');
>
> // readfile( $zipdir . '/' . $zipfilename );
> if ( ( $fp = fopen( $zipdir . '/' . $zipfilename, 'rb' ) ) === false )
> exit;
> @fpassthru( $fp );
> @fclose( $fp );
>
> ---
>
> This all works fine. The commented out readfile() _also_ worked
> fine, but I've been trying to see how to get past my problem, which
> is:
>
> Once the file is sent to the output stream, the script seems to
> halt.
>
> I want to get the script to continue rendering a 'Thank you for
> downloading...'-type page, but nothing seems to happen after the
> readfile() or fpassthru().
>
> In practical terms, this is nothing more than an annoyance. The
> end user gets the requested ZIP file, but nothing else can be done in
> the script. (I have a 'busy' graphic that I unhide on form submission
> that I want to hide again upon completion.)
>
>
> What am I forgetting? Is there something in the header man pages
> that I've missed?
>
> Other examples I've searched for online seem to imply that script
> execution should continue.
>
>
> Any thoughts would be appreciated.
>
> -Joe
>

Joe,

Yes, that's normal. When the browser gets the zip file content header,
it won't be expecting html (or vice versa). File downloads are
typically a "dead end" - nothing more in that window following the download.

The easy way around this is to display your page again with a clickable
link to the zip file. That way you can display HTML and yet allow the
user to download the file. And if you want to automate it, a little
javascript can be used to start the actual download.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Downloads, headers and scripts not finishing...

am 20.10.2007 23:09:17 von Dahak

On Sat, 20 Oct 2007 21:49:46 +0200, an orbiting mind-control laser
made Michael Fesser write:

>> I want to get the script to continue rendering a 'Thank you for
>>downloading...'-type page, but nothing seems to happen after the
>>readfile() or fpassthru().
>
>That would be an entirely different thing. The browser requests one
>resource and gets one, a ZIP file in this case. For another resource
>like an HTML page for example the browser would have to send another
>request, because you can't send two resources back at the same. One
>request, one response, one resource.
>
>> In practical terms, this is nothing more than an annoyance. The
>>end user gets the requested ZIP file, but nothing else can be done in
>>the script.
>
>It can't be done in the script because it can't be done in HTTP.
>
>> Other examples I've searched for online seem to imply that script
>>execution should continue.
>
>Of course the script execution can continue, but you can't send a second
>document back to the browser after the first requested file was sent.

Thanks for the quick reply.

Not being able to send anything else after the file was the bit I
was missing.


-Joe

Re: Downloads, headers and scripts not finishing...

am 20.10.2007 23:12:37 von Dahak

On Sat, 20 Oct 2007 16:11:43 -0400, an orbiting mind-control laser
made Jerry Stuckle write:

>Joe,
>
>Yes, that's normal. When the browser gets the zip file content header,
>it won't be expecting html (or vice versa). File downloads are
>typically a "dead end" - nothing more in that window following the download.
>
>The easy way around this is to display your page again with a clickable
>link to the zip file. That way you can display HTML and yet allow the
>user to download the file. And if you want to automate it, a little
>javascript can be used to start the actual download.


Thanks for the clarification.

I appreciate the help.

-Joe

Re: Downloads, headers and scripts not finishing...

am 21.10.2007 01:18:12 von Dahak

On Sat, 20 Oct 2007 16:11:43 -0400, an orbiting mind-control laser
made Jerry Stuckle write:

>File downloads are
>typically a "dead end" - nothing more in that window following the download.

On rereading this, I have to smile...

The current script provides options to select file format and
title to download and then generates the ZIP file and transfers it to
the client.

That window is actually still active and since the contents
haven't changed, I can then go ahead and select a second set and
download _that_, all without reloading the page.

Thanks, again.

-Joe