Plain text download with PHP shows in browser window and not as download
<p>In a kind of "step-by-step" / "wizard" setup I try to send a newly created plain text file to the user.</p>

<p>Problem ist that on localhost (windows, xampp) it works as intended but executed on the remote machine (hosted webspace) it gives the file contents as plain text in the browser window. (tested with same browser: chrome)</p>

<p>The file is a plain text file with a database backup inside (only a few entrys, not big) and was created one step before the download. Next step is deleting it and that works fine too. File itself is valid and all handles closed. Contents shown in the browser are the actual file with all lines.</p>

<p>$_POST sanitizing comes when download works, for simplifications unsanitized atm:</p>

<pre><code>&lt;?php
require_once('CSettings.php');
$s = new CSettings();
$file = $_POST['file'];

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: text/plain');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header("Pragma: public");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Length: ' . filesize($file));
    readfile($file);
}

?&gt;

&lt;form action="index.php" method="post" accept-charset="UTF-8"&gt;
    &lt;input type="submit" value="Delete backup file from the server" class="btn_red"&gt;
    &lt;input type="hidden" value="&lt;?php echo $file; ?&gt;" name="file"&gt;
    &lt;input type="hidden" value="delete_backup" name="view"&gt;
&lt;/form&gt;
</code></pre>

<p>Any suggestions? Fiddled around with content types already to no avail. Last resort would be zipping the whole file and try again but that would make it unnecessary complicated.</p>