0

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.

Papa Charlie
  • 625
  • 8
  • 31
  • Are you calling it twice? Have you already called it once and did it again? – Rudi Visser Jul 23 '13 at 08:09
  • Use `file_get_contents` for reading and `file_put_contents` for writing. Also FILE_APPEND mode appends the new content to the previous one. Empty your file, then retry. – Fracsi Jul 23 '13 at 08:09
  • forgiveness friends, I duplicated when written. file_put_contents is only one that duplicates the contents – Papa Charlie Jul 23 '13 at 08:11

1 Answers1

0

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.

Konsole
  • 3,447
  • 3
  • 29
  • 39
  • FILE_APPEND is not the problem, it just overwrites what already exists – Papa Charlie Jul 23 '13 at 08:28
  • FILE_APPEND is the problem. It appends the new content to the previous one. – Fracsi Jul 23 '13 at 08:33
  • 1
    FILE_APPEND will not overwrite what already exists. It will append the new value to the previous one. Here is a good explanation about FILE APPEND http://www.tizag.com/phpT/fileappend.php – Konsole Jul 23 '13 at 08:35