Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: display content after forcing download?

Tags:

php

header

I got a script like this

<?php

if (file_exists("./foo.txt")) {

    header("Content-type: application/force-download");
    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.

like image 797
John D. Avatar asked Jul 20 '10 11:07

John D.


1 Answers

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).

like image 88
Gumbo Avatar answered Sep 19 '22 12:09

Gumbo