Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    fwrite() - Content on New Lines

    Hello,
    I am following a tutorial teaching me about the multiple read/right operations.

    the code below works fine in that when i submit text into the form my textfile will update. the problem is that the updat is not on a new line, it is simply shown beside the old text.

    the tutorial said to add "\n" (as i did in my code below) however this is not working form me

    PHP Code:
    // open the file in append mode
      
    $file fopen($_SERVER['DOCUMENT_ROOT'].'/update/filetest05.txt''a');
      
    // write the contents after inserting new line
      
    fwrite($file"\n$contents");
      
    // close the file
      
    fclose($file);
      } 
    any help would be great.

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    On windows XP this works fine. Maybe you are using Mac, or Linux, i know that there could be different way to go to new line on those OS's.

    As for something else too you could check

    A list of possible modes
    for fopen(), that is that 'a' you are using in fopen. And you could maybe use other more convenient way.

    In your function instead of first setting \n$content, i would set $content\n beacouse 'a' mode

    Quote Originally Posted by php.net
    Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    So if it's first time your writing to file it will create empty line on the begining of the file, and new lines at the end of the $content, so you will have empty line at the begining of the file...

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    it depends on which application you use to view the file, notepad for example requires "\r\n" but many editors autodetect the newline type and will display the newline regardless.

    e.g. on windows I find I have to use "\r\n" if I want say notepad to display the newline whilst all linux editors seem happy with "\n".
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    im viewing the output on a linux server so the book said to only use "\n". It also said to put the \n infront.
    regradless i had already tryed all possible variotions and none worked.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Wait, you are saying after you echo the new text file its not correctly formatted or are you saying its not correctly formatted when viewing the text file?

    If you are saying that after echoing its not correctly formatted then this normal. New lines mean nothing in HTML, you need to use the nl2br function to maintain the new line characters.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    New lines mean nothing in HTML, you need to use the nl2br function to maintain the new line characters.
    YES! If you are viewing it in a browser, you will not see new lines there like aerospace said.

    Open your text file in some editor, or do a view source! If there is nothing in your text file, than probably your path is invalid.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •