Search code examples
phpfopenfwritefile-put-contents

fopen($filename, "a+") nor file_put_contents($filename, = $data.PHP_EOL, FILE_APPEND) seem to append to file


Trying to append file with either:

file_put_contents($filename, "\r\n" . $barcode_number.PHP_EOL, FILE_APPEND);

or

$myfile = fopen($filename, 'a+');

neither appending my file. Both are over writing old information
Things I have tried are in code comments

    $myfile = fopen($filename, 'a+') or die("unable to open file" . $filename);

    $barcode_number = $_POST['SBN'];
    //$newLine = PHP_EOL; 
    //echo  $temp.$barcode_number; die();

    fwrite($myfile, $barcode_number.PHP_EOL);
    fwrite($myfile, "\n");

    //file_put_contents($filename, "\r\n" . $barcode_number.PHP_EOL, FILE_APPEND);
    fclose($myfile);
    //echo "wrote " . $barcode_number . "to " . $filename; die();

Expected output is:

123456
123457
123458

actual output is:

123458

Solution

  • Your file_put_contents() call is formatted correctly, although I don't think you need the .PHP_EOL, since you're already prepending the data with "\r\n". So is your fopen()/fwrite()/fclose(). Is it possible that another script is opening this file, and thus locking the file?