views:

23

answers:

1

I know how to display output to a web browser (obviously) and I also know how to push a file for the client to download by setting the Content-Disposition and Content-Type headers and then pushing the file data. But I want to do both, similar to how many download pages work ("your file download will begin momentarily").

What technique is used to accomplish that? I was thinking about using a META HTTP refresh, but here's the thing... The web page is actually doing some fairly complex processing and I want to display the results of the processing to the user and push the results in file form to the user but only perform the processing once. I would like to avoid the HTTP refresh if possible. Is there any way to accomplish both of these with a single page request? Or am I going to have to write the file to the server and then serve it up on the second page request from the HTTP Refresh request?

A: 

Use an iframe in your output that will have a link to the file you want to download.

Output sample:

<div>Foo</div>
<iframe width="0px" height="0px" src="path/to/file"></iframe>

You cannot say "this part if for display and that one is for download" with the HTTP protocol.

However, you can "cache" the file in a temp directory, one that is inaccessible from the public view (outside the document root) and then output that document content through PHP (ex: file_get_contents or something). That temp folder can then be cleaned by an API that could simply scan that folder and delete any file older than a certain time (see filemtime())

This cleanup can be done every time a new file is created, thus keeping the size of the folder at a reasonable value.

Yanick Rochon
Ok I see how that would work... but that would also create a second request to the server that I would have to cache the file. I guess I should say that the file isn't a real file - it is data from SQL queries. I don't want to save the file to disk if I don't have to, and I don't want to perform the SQL query twice either. Is there a way to do both without a second trip to the server while still displaying something to the user?
Michael Bray
I'm afraid not. see my edits
Yanick Rochon
Thanks! I found another way to cache that I'm happier with - I just stuck the file in a Session variable and then redirect with the IFRAME telling the page to only send the data from the session variable, then erase the session variable. Works like a champ!!
Michael Bray
yeah :) that works too! As long as your file/data is not too big, in this case you'll burst the memory usage limit (since the session data is completely loaded into memory for each requests)
Yanick Rochon