Inserting text into an existing file (at a specified line).

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoonya
    New Member
    • May 2007
    • 160

    #1

    Inserting text into an existing file (at a specified line).

    suppose i want to write on the 5th line of any file then how to implement this in php??

    shoonya
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    Hi,

    did you take a look at php.net ? try fopen

    Purple

    Comment

    • shoonya
      New Member
      • May 2007
      • 160

      #3
      Originally posted by Purple
      Hi,

      did you take a look at php.net ? try fopen

      Purple
      fopen is for opening the file
      i am talking about writing on any given line
      (read the question first)

      shoonya

      Comment

      • Purple
        Recognized Expert Contributor
        • May 2007
        • 404

        #4
        Hey thanks for the redirect to the question shoonya,

        fopen is not a bad place to start if you want to manipulate files. If you read the page on php.net as initially suggested you will find the following code which almost does what you want..

        The following cut directly from php.net - posted there by naidim at gmail dot com

        While PHP does not have a function to insert text into the middle of a file, it is not that complicated to do.

        [PHP]<?php
        function addRSSItem($rss File, $firstItem, $item){
        // Backup file
        if(!copy($rssFi le, 'backup.rss')) die('Backup failed!');
        // Store file contents in array
        $arrFile = file($rssFile);
        // Open file for output
        if(($fh = fopen($rssFile, 'w')) === FALSE){
        die('Failed to open file for writing!');
        }
        // Set counters
        $currentLine = 0;
        $cntFile = count($arrFile) ;
        // Write contents, inserting $item as first item
        while( $currentLine <= $cntFile ){
        if($currentLine == $firstItem) fwrite($fh, $item);
        fwrite($fh, $arrFile[$currentLine]);
        $currentLine++;
        }
        // Delete backup
        unlink('backup. rss');
        }

        $data = " <item>\n<title> $_POST['title]</title>\n".
        " <description>$_ POST['description']</description>\n" .
        " <pubDate>$_PO ST['date']</pubDate>\n".
        " <link>http://www.site.com/mp3s/".
        basename($_FILE S['fullPath']['name'])."</link>".
        " <enclosure url=\"http://www.site.com/mp3s/".
        basename($_FILE S['fullPath']['name']).
        "\" length=\"$_FILE S[fullPath][size]\" type=\"audio/mpeg\" />".
        " </item>\n";
        addRSSItem('/var/www/html/rss/podcast.rss',20 ,$data);
        ?> [/PHP]

        Purple

        Comment

        • epots9
          Recognized Expert Top Contributor
          • May 2007
          • 1352

          #5
          [PHP]$arrFile = file("./file.txt");[/PHP]
          will open the file, store each line in a separate array slot.
          line 1 = $arrFile[0] and so on.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            That's one of those bugging things in PHP. Anyone up for a class that can do it hiding the details so people can use it all the time they want to edit files?

            Comment

            • ronnil
              Recognized Expert New Member
              • Jun 2007
              • 134

              #7
              how bout http://pear.php.net/package/File/ ?? at least a very simple routine could be written using PEAR::File

              Comment

              • shoonya
                New Member
                • May 2007
                • 160

                #8
                Originally posted by ronnil
                how bout http://pear.php.net/package/File/ ?? at least a very simple routine could be written using PEAR::File
                @ purple

                i hope you wont mind in explaining why there are two fwrite on line 16 nd 17 ??

                cheers :)
                shoonya

                Comment

                • Purple
                  Recognized Expert Contributor
                  • May 2007
                  • 404

                  #9
                  I would guess from the description of the code snippet:

                  While PHP does not have a function to insert text into the middle of a file, it is not that complicated to do.
                  it is inserting a record - you are trying to update a record - so change to contents and rewrite it..

                  Purple

                  Comment

                  • shoonya
                    New Member
                    • May 2007
                    • 160

                    #10
                    got it
                    thanks purple
                    ;)

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Changed thread title to better describe the problem.

                      Comment

                      Working...