0

In a kind of "step-by-step" / "wizard" setup I try to send a newly created plain text file to the user.

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)

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.

$_POST sanitizing comes when download works, for simplifications unsanitized atm:

<?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);
}

?>

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

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.

Idenwen
  • 13
  • 4

1 Answers1

0

One solution that may work is to use the download html attribute, However, it doesn't seem to be supported in Safari.

Download file when clicking on the link (instead of navigating to the file):

<a href="/images/myw3schoolsimage.jpg" download>

From: http://www.w3schools.com/tags/att_a_download.asp

This however may require you to put the download in a separate script.

You may also look here: How to Automatically Start a Download in PHP?

Community
  • 1
  • 1
Adam Forbis
  • 461
  • 3
  • 11
  • Yea, will try that as a workaround. Thanks for the headsup the "download" attribute was unknown to me. But still trying to figure out why that thing will not download itself on the live machine.... – Idenwen Jul 01 '16 at 13:36
  • @Idenwen Another useful link: http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/ – Adam Forbis Jul 01 '16 at 13:42
  • Thanks, thats where I got `header("Cache-Control: must-revalidate, post-check=0, pre-check=0");` from to work around IE Problems (but the users in question only use chrome [kind of intranet application]) – Idenwen Jul 01 '16 at 13:53
  • @Idenwen I think the important part of that was `header("Content-Type: application/octet-stream");` Which I believe indicates that it is a generic file, and if the file extension is set on the filename they should be able to open it, but the browser shouldn't try. – Adam Forbis Jul 01 '16 at 14:21