Becky Becky - 2 months ago 13
PHP Question

Add data to text with print_r()

I have a text file and each time a visitor access this page I am doing a print_r() to it.

How can I use print_r() to add data to my

file.txt
text file ? Currently the text is overwritten and stores only last print_r().

$output = print_r($data, true);
file_put_contents('file.txt', $output);

Answer

You can use the FILE_APPEND flag:

file_put_contents('file.txt', $output, FILE_APPEND);

FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it.