1

In my project there is a part where when the user click a button, it downloads a .txt file containing some strings. I run this code in a file .php. The .php file creates ever the .txt file but it doesn't download it into the broswer.

<?php


$outString = "write here";

$file = "test.txt";
$txt = fopen($file, "w") or die("Unable to open file!");
fwrite($txt, $outString);
fclose($txt);

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

?>
  • Not reproductible. The given code works as expected. Note: you can use `file_put_contents($file, $outString);` instead of `fopen`+`fwrite`+`fclose`. – Syscall Mar 24 '21 at 10:26
  • 1
    [Create file for download without saving on server with PHP](https://stackoverflow.com/questions/10966433/create-file-for-download-without-saving-on-server-with-php) – Definitely not Rafal Mar 24 '21 at 10:29
  • `but it doesn't download it into the broswer.`...so what _does_ it do then? An error? Or some other unexpected behaviour? Please clarify the situation. – ADyson Mar 24 '21 at 10:39

0 Answers0