Following php code I use to download a string to clients disk.
$str = "Some pseudo-random text spanning multiple lines";
header('Content-Disposition: attachment; filename="sample.txt"');
header('Content-Type: text/plain'); # Don't use application/force-download - it's not a real MIME type, and the Content-Disposition header is sufficient
header('Content-Length: ' . strlen($str));
header('Connection: close');
echo $str;
The code I took from TML here
It works perfect on my local XAMP dev-server. But when I transfer it to the server of my internet provider the file is not stored on disk but present on the screen.
Maybe it`s an issue of some wrong or missing php settings of the internet server, I do not know.
Any help is appreciated.
EDIT : Okay I got the problem. After checking PHP log errors I found an echo
statement before setting the header()
. I removed that echo
and it works now as expected.
This question is different to the one mentioned below. I do not want to fix headers. I need to follow the rules of writing php code properly.