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?
Use an iframe in your output that will have a link to the file you want to download.
Output sample:
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_contentsor 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.