Search code examples
phpapachefilefwrite

file_put_contents duplicating content


I am using file_put_contents writing to a file and I'm getting duplicate content ... written 2x and 1x litura

file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test.tmp', '1 ', FILE_APPEND);

contents of test.tmp

11

already restarted apache and I am accessing directly without htaccess.


Solution

  • You are using file_put_contents in Append mode so 1 will be appended every time you run the script. Use the code without 3rd parameter.

    file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test.tmp', '1 ');
    

    Now the output of the test.tmp file will always be 1.