I’m trying to write data to a simple txt file in PHP and open it so the user can download it. I’d like to keep it very lightweight, when I run this I get:
Warning: fopen(testFile.txt) [function.fopen]: failed to open stream: File exists in /home/user/script.php on line 9
Here is my sample code:
<?php
$File = "sample.txt";
$write = fopen($File, 'w') or die();
$stringData = "asdfjkl;";
fwrite($write, $stringData);
fclose($write);
$open = fopen($File, 'x') or die();
fclose($open);
?>
Thank you!
Use file_put_contents to write to the file:
Then when you want to output the contents of the file use:
If you want the file contents to be returned as a string instead of output immediately use file_get_contents instead of readfile