Re: How do I send HTML and a file for 'Save-As' at the same time?

This is a discussion on Re: How do I send HTML and a file for 'Save-As' at the same time? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; If you look at the html code of the page you're talking about, you'll notice the following: <...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-29-2005
Daedalus.OS
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

If you look at the html code of the page you're talking about, you'll notice
the following:

<META HTTP-EQUIV="refresh" content="5;
URL=http://umn.dl.sourceforge.net/sourceforge/vnc-tight/tightvnc-1.2.9_x86_viewer.zip">

This line makes the file to download 5 seconds after the page is loaded.
This is often used for redirection, but since in this case it refresh (or
redirect) to a zip file, then this doesn't replace the page but prompt to
save the file instead.

Dae


<edykstra@virtualcad.com> wrote in message
news:1117327839.547641.216760@o13g2000cwo.googlegr oups.com...
> Hello,
>
> If you point your browser to this page,
>
> http://prdownloads.sourceforge.net/v...use_mirror=umn
>
> the server sends you the HTML page, and very soon afterwards, you are
> prompted to do a 'Save-As' for a file to download.
>
> How do they do that without the classic "Headers already sent" error?
>
> In my application, I need to do something very similar.
>
> Thanks in advance for any help!
>
> Eric
>



Reply With Quote
  #2 (permalink)  
Old 05-29-2005
edykstra@virtualcad.com
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

Hello,

OK .. that makes sense. I didn't realize it was just waiting 5 seconds
to serve me a file that already exists. I thought it was processing a
file and serving it to me as soon as it was ready, which is what I need
to do.

The file I am going to serve the User can take anywhere from 3 to 30
seconds to create dynamically. If I use a value too low, the page will
refresh to 'Object not found'. If the value is too high, the User is
waiting longer than they should have to.

Here is what I want my application to do:

1. User clicks on a link.
2. Server sends HTML page explaining it may take up to 30 seconds.
3. Server sends file prompting a 'Save-As' as soon as it is ready.

I would like it better if there was no User interaction other than step
1.

Is this possible?

Eric

Reply With Quote
  #3 (permalink)  
Old 05-29-2005
Daedalus.OS
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

Instead of refreshing to a static files, refresh to a php file that will
send back the file.

Lets say when the user click the link it brings him to some_page.php. In
this page you would put this code:

<META HTTP-EQUIV="refresh" content="1;
URL=http://yoursite.com/some/folder/file_gen.php">

file_gen.php should now generate the file and sent it back to be downloaded.
Without more detail about this generated file I can't tell you how exactly
to send it back. But here is a simple exemple that would return a
dynamically created javasctipt to be downloaded:

// The file is generated and its handler is contained by $file...
header('Content-type: application/javascript');
header("Content-length: ".filesize($file));
header('Content-Disposition: attachment; filename="destination_name.js"');
readfile($file);

You will find some more exemple in the user comments of the header() and
fread() manual pages:
http://ca3.php.net/manual/en/function.fread.php
http://ca3.php.net/header

Other function that might be useful:
http://ca3.php.net/manual/en/function.fwrite.php
http://ca3.php.net/manual/en/function.tempnam.php
http://ca3.php.net/manual/en/function.tmpfile.php

Dae

<edykstra@virtualcad.com> wrote in message
news:1117333937.281656.130930@g44g2000cwa.googlegr oups.com...
> Hello,
>
> OK .. that makes sense. I didn't realize it was just waiting 5 seconds
> to serve me a file that already exists. I thought it was processing a
> file and serving it to me as soon as it was ready, which is what I need
> to do.
>
> The file I am going to serve the User can take anywhere from 3 to 30
> seconds to create dynamically. If I use a value too low, the page will
> refresh to 'Object not found'. If the value is too high, the User is
> waiting longer than they should have to.
>
> Here is what I want my application to do:
>
> 1. User clicks on a link.
> 2. Server sends HTML page explaining it may take up to 30 seconds.
> 3. Server sends file prompting a 'Save-As' as soon as it is ready.
>
> I would like it better if there was no User interaction other than step
> 1.
>
> Is this possible?
>
> Eric
>



Reply With Quote
  #4 (permalink)  
Old 05-29-2005
edykstra@virtualcad.com
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

Dae,

Thanks! I came up with the same idea late last night while I was trying
to get to sleep. What I will do is refresh the page over and over until
the file is ready. On each refresh, I can show a 'progress bar'. When
the file is ready, the following refresh will send the file.

Eric

Reply With Quote
  #5 (permalink)  
Old 05-29-2005
Daedalus.OS
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

I don't understand why you want to refresh over and over ? If you create
your file within the file_gen.php file (from my previous exemple) and then
when the creation process is finished make the script sends the required
headers followed by fread('file') ... when you will do the refresh to that
file the browser should wait for the headers before taking any action (as
long as the browser request doesn't timed out). When it will receive it
since the header will specify Content-Disposition: attachment;
filename="destination_file_name.ext", then it should prompt to save the
file.

Dae


<edykstra@virtualcad.com> wrote in message
news:1117368506.468500.139130@g47g2000cwa.googlegr oups.com...
> Dae,
>
> Thanks! I came up with the same idea late last night while I was trying
> to get to sleep. What I will do is refresh the page over and over until
> the file is ready. On each refresh, I can show a 'progress bar'. When
> the file is ready, the following refresh will send the file.
>
> Eric
>



Reply With Quote
  #6 (permalink)  
Old 05-29-2005
John Dunlop
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

Daedalus.OS wrote:

> Content-Disposition: attachment; filename="destination_file_name.ext"


do browsers on the whole take heed of that line do you know?

--
Jock
Reply With Quote
  #7 (permalink)  
Old 05-29-2005
Daedalus.OS
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

This should work in all recent browser. Content-Disposition is stated as
experimental but is implemented in most (if not all) major browser.

I have tested it with IE6, FireFox 1.0, Netscape7.02, Opera7.5.4,
Mozilla1.7.3
I know there were issues on some browser that would result in displaying the
file if the content-type was a known text type or that would download the
file with an php or html extension.

I don't remenber wich browser and version have wich issue (I think IE5.5 was
one of those and FireFox 0.9 was the other), but I remember I was using
application/octet-stream as content-type for text file to solve this.

Dae


"John Dunlop" <usenet+2004@john.dunlop.name> wrote in message
news:MPG.1d0415903871ab8b989688@news.ntlworld.com. ..
> Daedalus.OS wrote:
>
>> Content-Disposition: attachment; filename="destination_file_name.ext"

>
> do browsers on the whole take heed of that line do you know?
>
> --
> Jock



Reply With Quote
  #8 (permalink)  
Old 05-30-2005
edykstra@virtualcad.com
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

Dae,

Without a request from the client for another 'page' won't I receive
the 'Headers already sent' error?

Eric

Daedalus.OS wrote:
> I don't understand why you want to refresh over and over ? If you create
> your file within the file_gen.php file (from my previous exemple) and then
> when the creation process is finished make the script sends the required
> headers followed by fread('file') ... when you will do the refresh to that
> file the browser should wait for the headers before taking any action (as
> long as the browser request doesn't timed out). When it will receive it
> since the header will specify Content-Disposition: attachment;
> filename="destination_file_name.ext", then it should prompt to save the
> file.
>
> Dae
>
>
> <edykstra@virtualcad.com> wrote in message
> news:1117368506.468500.139130@g47g2000cwa.googlegr oups.com...
> > Dae,
> >
> > Thanks! I came up with the same idea late last night while I was trying
> > to get to sleep. What I will do is refresh the page over and over until
> > the file is ready. On each refresh, I can show a 'progress bar'. When
> > the file is ready, the following refresh will send the file.
> >
> > Eric
> >


Reply With Quote
  #9 (permalink)  
Old 05-30-2005
Ivan Omelchenko 608308824
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

Daedalus.OS пишет:
> Instead of refreshing to a static files, refresh to a php file that will
> send back the file.
>
> Lets say when the user click the link it brings him to some_page.php. In
> this page you would put this code:
>
> <META HTTP-EQUIV="refresh" content="1;
> URL=http://yoursite.com/some/folder/file_gen.php">
>
> file_gen.php should now generate the file and sent it back to be downloaded.
> Without more detail about this generated file I can't tell you how exactly
> to send it back. But here is a simple exemple that would return a
> dynamically created javasctipt to be downloaded:
>
> // The file is generated and its handler is contained by $file...
> header('Content-type: application/javascript');
> header("Content-length: ".filesize($file));
> header('Content-Disposition: attachment; filename="destination_name.js"');
> readfile($file);
>

Hows about $file that weight more then 8Mb ?
You can't do it with movie, for example.
Reply With Quote
  #10 (permalink)  
Old 05-30-2005
Daniel Tryba
 
Posts: n/a
Default Re: How do I send HTML and a file for 'Save-As' at the same time?

In comp.lang.php Ivan Omelchenko 608308824 <news@omelchenko.com> wrote:
>> // The file is generated and its handler is contained by $file...
>> header('Content-type: application/javascript');
>> header("Content-length: ".filesize($file));
>> header('Content-Disposition: attachment; filename="destination_name.js"');
>> readfile($file);
>>

> Hows about $file that weight more then 8Mb ?
> You can't do it with movie, for example.


Why do you think that? readfile() "Reads a file and writes it to the
output buffer." [http://nl3.php.net/readfile]. So it doesn't use any
memory in php other than:
-the buffers to read the file and write to the output buffer.
-the output buffer _if_ output buffering in php is set to on, not
something you want to do in these kind of scripts...

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0