PHP fwrite不使用追加模式更新文件

The following code is working but it doesn't update the contents of the file it created.

I can see that the file contents have changed (the size increased) but when I download the file from my server it's empty. the file is chmod to 666 and its parent directory as well.

its a linux server running Apache and PHP. I've also tried using fflush to force it to flush the contents.

<?php

header("Location: http://www.example.com");
$handle = fopen("log.txt", "a");
foreach($_POST as $variable => $value) {
   fwrite($handle, $variable);
   fwrite($handle, '=');
   fwrite($handle, $value);
   fwrite($handle, '
');
}

fwrite($handle, '
');
fflush($handle);
fclose($handle);

?>

what is the problem?

Thanks!

I think a good practice is to check if a file is writable with is_writable then if it can be opened by checking the value returned by fopen, by the way your code is right.

Try this:

$filename = "log.txt";
$mode = "a";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, $mode)) {
         echo "Cannot open file ($filename)";
         exit;
    }

    foreach($_POST as $variable => $value) {
       fwrite($handle, $variable);
       fwrite($handle, '=');
       fwrite($handle, $value);
       fwrite($handle, '
');
    }

    fwrite($handle, '
');
    fflush($handle);
    fclose($handle);
    echo "Content written to file ($filename)";

} else {
    echo "The file $filename is not writable";
}