-2
<?php
$file = fopen("testing.txt","w");
$txt = "Who let the dogs out?! \r\n";
echo fwrite($file, $txt);

header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file.txt");
header("Content-Type: text/plain; ");
header("Content-Transfer-Encoding: binary");

readfile('testing.txt');
?>

First issue: I read on the PHP website (http://php.net/manual/en/function.fopen.php) that there could be issues with line-ending, but when testing.txt has been downloaded and is opened, the following line appears with some numbers added in front:

25Who let the dogs out?!

Second issue: I noticed the file name is Resource id #3.txt instead of testing.txt. A Google search reveals that that error message is commonly associated with MySQL/PHP, but it is only PHP for my case.

taylorswiftfan
  • 1,371
  • 21
  • 35
  • 1
    instead of `echo fwrite($file, $txt);` just write `fwrite($file, $txt);` and `header("Content-Disposition: attachment; filename=$filename.txt");` – Alive to die - Anant Aug 15 '16 at 06:56
  • also, you are using `$file` for the file name, that's a file resource and as expected, you get its string representation as your file name – Ibrahim Lawal Aug 15 '16 at 06:59

1 Answers1

1

I guess, it is because you have this line in your code

echo fwrite($file, $txt);

Try this code

fwrite($file, $txt);

and correct this

$filename = 'testing.txt';
header("Content-Disposition: attachment; filename=$filename.txt");
Michail M.
  • 735
  • 5
  • 11
  • Okay this is pure embarrassment on my end. Can't believe the solution is that simple (I thought it is something to do with CR)! Also I *just* noticed that I omitted fclose() by accident. – taylorswiftfan Aug 15 '16 at 06:59