Search code examples
phpphp4

Why does file_put_contents not append data to a file?


I have a complex php4 code and want to write something to a file, using file_put_contents, like the following:

$iCounter = 0;
foreach blah...
    ... code
    file_put_contents("/tmp/debug28364363264936214", "test" . $iCounter++ . "\n", FILE_APPEND);
    ...code

I am using FILE_APPEND to append the data to the given file, I am sure that what I print does not contain control characters, the random number sequence makes sure the file is not used in any other context, and the counter variable (only used for the printout) is used to control how often file_put_contents is called.

Executing the code and looking at the file content I just see

test8

while I expect to see

test1
test2
test3
test4
test5
test6
test7
test8

Is there a problem with my syntax? Maybe the code does not work for php4? What else can I use instead?

P.S. I am working on a very large, quite ancient php4 project, so there is no way to recode the project in php5. Not within 2 years...


Solution

  • use fopen(); file_put_contents() is php5.

    $file = fopen("/tmp/debug28364363264936214", "a+");
    fwrite($file, "test" . $iCounter++ . "\n");
    fclose($file);