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 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    Manassas, VA, USA
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add line of text to beginning of file

    How do you add a line of text to the beginning of a file? I'm able to put it at the end of the file, but i need the new line at the beginning.

    -Andrew
    Get the best Web Design books called Visual QuickStart Guides at the Peachpit Press
    Ithium Web Hosting

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $file = 'locationOfFile';

    $array = file($file);

    $str = "new text";




    for ($i = 0; $i<count($array); $i++) {

    $str .= $array[$i];

    }

    $fp = fopen($file, "w+");
    fwrite($fp, $str);
    fclose($fp);

    Didn't test it... so something like that...

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    you were possibly using a instead of w

    PHP Code:
    $fp fopen($file"w+"); 
    the w+ writed to the start of a file and if not available then it creates, whereas a+ points to the end of the file

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    nooooooooo w+ will delete the contents of the file if exists , else create a new file.

    You can't actually write to the beginning of a file as such , you can open a file and fseek() to the beginning of the file , but you will be overwriting the existing data at that part of the file.

    duniyadnd 's example is a typical workaround , read the file into memory , add your data and re-save.
    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)

  • #5
    Regular Coder
    Join Date
    May 2003
    Location
    Manassas, VA, USA
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that's what i figured. The reason I originally asked was because I was trying to code my own blog, but I went instead with WordPress, so it doesn't matter anymore. Thanks for the info tho, I'm sure I'll use it in the future.

    -Andrew
    Get the best Web Design books called Visual QuickStart Guides at the Peachpit Press
    Ithium Web Hosting


  •  

    Posting Permissions

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