PHP: display content after forcing download?
I got a script like this
<?php
if (file_exists("./foo.txt")) {
header("Content-type: application/force-downl开发者_Go百科oad");
header("Content-length: ".filesize("./foo.txt"));
header('Content-Disposition: attachment; filename="foo.txt"');
readfile('./foo.txt');
}
print "HEY!";
?>
and I want the "Hey" to be written to the page after the forced download, how is that possible?
I see download pages that are like "If your download does not start automatically click here", so it's content and the download? The hey
in my example is included in the text file, so it doesn't work.
This would only be possible with a multipart message. But unfortunately there are not many browser that support multipart messages in HTTP (I think Opera is the only one).
That’s why it’s common practice to send the download page with a meta refresh redirect to have both a textual message and a download action (although it’s in the opposite order).
You can do it with META REFRESH like this:
<meta http-equiv=REFRESH CONTENT="5; url=http://your_domain/your_file">
This is not possible this way. (At least as long as we don't have multi-part requests.)
The pages you mention redirect to the "if your download does not start...." page, and trigger the download from there, usually using JavaScript to open the download in a new window.
Display the message on another page .. then provide a link to download the file or redirect the browser to the php file that forces the download ..
onClick="setTimeout(location.reload.bind(location), 15000);"
This will wait 15 seconds (15,000 milliseconds), then call the location.reload function, which is a built-in function to refresh the page.
精彩评论