Search code examples
phpblank-line

PHP_EOL and "\n" leave 1st line blank


I have simple code that saves data:

$data = $_POST["data"];
$userID = $_POST["userID"]; 

if (empty($data)){
   } else {
    $theFile = fopen("Data/" . $userID . ".txt", "a+");
    fwrite($theFile, PHP_EOL.$data);
    fclose($theFile);      
}

Data works fine for saving the data, but there is always a blank line in line 1. I've tried switching to "\n" and the problem persists. Is there anything else I can try?


Solution

  • Change:

    fwrite($theFile, PHP_EOL.$data);
    

    With:

    fwrite($theFile, $data.PHP_EOL);
    

    Otherwise you'll have this first line empty as the first written char is a \n