SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Add lines to a file

    Hope someone can help. To explain the situation, I have a file called index.php [writable on the server] with the following content:

    PHP Code:
    <?php
    echo "Angela";
    echo 
    "Ann";
    echo 
    "Betty";
    echo 
    "Kate";
    ?>
    What I am looking for is using another .php file to add extra lines to index.php, and change it to something like:

    PHP Code:
    <?php
    echo "Angela";
    echo 
    "Ann";
    echo 
    "Aura";     // added line via PHP command
    echo "Betty";
    echo 
    "Kate";
    ?>
    I found this , the problem is that it describes how to add lines at the end of a .txt file while I need to insert lines among existing ones of a .php file.

    Is that possible?

    Thanks a lot in advance!

  2. #2
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My approac would be to:

    1) read the existing file into an array
    2) add the new line(s) to the array
    3) sort the array intio the order you want the items to be in the new file
    4) write the array to the new file

    I would also have a step which makes a back up of the existing file before trying to over-write it, just in case the write fails.

    Mike

  3. #3
    PHP Brainiac dg_den_golotyuk's Avatar
    Join Date
    Jul 2006
    Location
    Kiev, Ukraine
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually there is one more decision for this issue. It is to use fseek() file function. But the file handling in PHP is not so powerfull to get this feature work so easy. Anyway:
    PHP Code:
    <?php

    // Will be the number of the line you are inserting new string line AFTER (first line number is 1)
    define('POSITION'3);
    // Will be the new line text (without ending \n symbol)
    define('NEW_LINE''new test line');

    $f fopen('t.txt''r+');

    for ( 
    $i 0$i POSITION$i++)
    {
        
    fgets($f);
    }

    $file_end fread($ffilesize('t.txt'));
    fseek($f, - (strlen($file_end)), SEEK_END);

    fwrite($fNEW_LINE "\n");
    fwrite($f$file_end);
    fclose($f);

    ?>
    DG [Den Golotyuk], Lead Developer
    Chestnut Software
    Avoid web outsourcing scams!
    Click here
    for a free downloadable report

  4. #4
    SitePoint Zealot _theworks's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    splitting it into an array would probably be the easiest solution...

    PHP Code:

    $lines 
    file('yourfile.php');
    $line_count count($lines)-1
    $new_line 
    "bobby";
    $buff'';

    for (
    $i 0$i <= $line_count-1$i++) {
        
    $buff.= $lines[$i]."\n";
    }

    $buff.= $new_line."\n".$lines[$line_count];
    file_put_contents('yourfile.php',$buff); 
    somthing like that anyway..

    EDIT: if you want to sort the array look into the sort() function

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys,
    This is already helpful, I see like I'd be able to use both solutions, however would it be possible to set the place to insert the new line(s) by particular word / phrase in the existing file rather than by line number?

    Many thanks!

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted, thanks to your great input!

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there away to control the result of a file_put_contents operation? meaning that if for some reason it went unsuccessful, can I set any error messages to display status?

  8. #8
    PHP Brainiac dg_den_golotyuk's Avatar
    Join Date
    Jul 2006
    Location
    Kiev, Ukraine
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can do smth like this:
    PHP Code:
    <?php

    if ( @file_put_contents('file.txt'$buffer) )
    {
        echo 
    'operation successful';
    }
    else
    {
        echo 
    'Operation failed';
    }

    ?>
    DG [Den Golotyuk], Lead Developer
    Chestnut Software
    Avoid web outsourcing scams!
    Click here
    for a free downloadable report


Bookmarks

Posting Permissions

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