start new line when append in file writing

Discussion in 'PHP' started by Rasputin, Dec 10, 2007.

  1. #1
    Hi, dim question I think but can someone tell me how to make the following start a new line each time it writes to the text file...

    <?php
    $myFile = "information.txt";
    $fh = fopen($myFile, 'a');
    $stringData = $_GET['id'];
    $stringDate = date('dS F Y');
    fwrite($fh, $stringData);
    fwrite($fh, $stringDate);
    fclose($fh);
    ?>

    I tried adding \n but it didn't seem to work...

    Cheers!
     
    Rasputin, Dec 10, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    \n should work.
    
    $stringData = "{$_GET['id']}\n";
    $stringDate = date('dS F Y') . "\n";
    
    PHP:
    If that doesn't work, try \r\n instead.
     
    nico_swd, Dec 10, 2007 IP
  3. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    \n is for *nix, \r\n is for Windows.
    So, unix version:
    
    $myFile = "information.txt";
    $fh = fopen($myFile, 'ab');  //<- read about 'b' flag at http://php.net/fopen
    $stringData = $_GET['id'];
    $stringDate = date('dS F Y');
    fwrite($fh, $stringData."\n");
    fwrite($fh, $stringDate."\n");
    fclose($fh);
    
    PHP:
     
    codesome, Dec 10, 2007 IP
  4. Rasputin

    Rasputin Peon

    Messages:
    1,511
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Two very useful answers, thanks
    I guess I had missed the concatenation '.'

    Cheers
     
    Rasputin, Dec 10, 2007 IP