How to Add Data to A Txt File?

Discussion in 'Programming' started by Magestry, Nov 19, 2008.

  1. #1
    Hey guys,

    I have made a funny dogs site and I am making an admin panel so that I can easily update things over the internet, but I would like to know how I can just add a line of data to a txt file, the text file containts many variables so it is something like this:

    <?php
    $var1 = Hello
    $var2 = Bye
    ?>

    (well not that exactly lol) but I want to know how I can just add one more variable by a form just before the ?> at the end so that it counts as a variable... or just something so that I can add <?php var3 = Lala ?> to the bottom of the file, just via form.

    Basically I need help in making something that can add a line of code to the bottom of a txt file, how do I do this?

    Thanks.
     
    Magestry, Nov 19, 2008 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    look into fopen & fwrite functions.
     
    happpy, Nov 19, 2008 IP
  3. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I found a mini script but as I want to add variables, I have to add " and ' but when I look at in on the txt file, it comes up with /" or /' how do I change this?

    Thanks.
     
    Magestry, Nov 19, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Frist of all 'Hello' and 'Bye' must be put in quotes, becuase they are string, otherwise php will consider them constants, and issue warning if not found, put as this:

    $var = "Hello";


    Now adding more to existing file:

    Method 1

    $data = file_get_contents("filename.php");
    $pos = strpos($data,"?>");
    
    $new_data = '$var3 = "Go ahead";'; //this is new value to be added
    
    $datatowrite = substr($data,0,$pos+1)."\r\n$new_data\r\n?>";
    
    $f = fopen("filename.php","w");
    fwrite($f,$datatowrite);
    fclose($f);
    PHP:

    Method 2

    $new_data = '$var3 = "Go ahead";'; //this is new value to be added
    
    $array = explode("\n",trim(file_get_contents("filename.php")));
    $key = array_search("?", $array);
    if($key === FALSE)
        print "File does not have ?&gt;";
    else {
        $array[$key] = $new_data;
        $array[]= "?>";
    
        $datatowrite = implode("\n",$array);
    
        $f = fopen("filename.php","w");
        fwrite($f,$datatowrite);
        fclose($f);
    }
    PHP:

    I hope it helps.
     
    Vooler, Nov 20, 2008 IP
  5. mortgageanddebthelp

    mortgageanddebthelp Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I really don't know about this topic .............................please explain more about above topic
     
    mortgageanddebthelp, Nov 20, 2008 IP
  6. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #6
    I did it, thanks guys for all your help!
     
    Magestry, Nov 20, 2008 IP
  7. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #7
    Vooler, Nov 20, 2008 IP
    Magestry likes this.
  8. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Thank you load Vooler,
    +rep coming your way!
     
    Magestry, Nov 21, 2008 IP
  9. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #9
    \r\n seems used by some text editors. What if i remove it from the code in method 1? What is the effect?
     
    cipals15, Nov 21, 2008 IP