overwrite the text in text file

Discussion in 'PHP' started by MrPJH, Nov 19, 2009.

  1. #1
    i need a little help i wrote a script in PHP that inputs data from form and write it to a text file it works but i want to remove hole data
    for example i sent through form "i sent through form"
    and if next time if i send "need help" it ll overwrite "need helprough form" red will be remain as it is
    any help
    code is bellow
    
    <?
    $receivedFromFlashData = $_POST['ID'];
    
    $myTextFileHandler = @fopen("file.txt","r+");
    
    
    $txtfileArray = @file("file.txt");
    
    if($myTextFileHandler){
    print("txtFile is opened\n");
    
    
    $writeInTxtFile = @fwrite($myTextFileHandler,"$receivedFromFlashData");
    if($writeInTxtFile){
    
    $writeStatus = "writing to textfile was a succes";
    
    print("&writeStatus=$writeStatus");
    
    
    }else{
    $writeStatus = "writing to textfile was a big failure";
    print("&writeStatus=$writeStatus");
    };
    @fclose($myTextFileHandler);
    
    }else{
    print("opening txtfile has failed\n");
    };
    print("&receivedData=$receivedFromFlashData");
    ?>
    PHP:
    it overwrite the beginning of data not all data
     
    Last edited: Nov 19, 2009
    MrPJH, Nov 19, 2009 IP
  2. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try replacing "r+" on line 4 for "w"
     
    emed, Nov 19, 2009 IP
  3. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you want to overwrite just use 'w' instead of r+
    if you want to add to the content use 'a'..
     
    xenon2010, Nov 19, 2009 IP
  4. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #4
    thanks for your help
    using a and w mods problem is resolved
     
    MrPJH, Nov 20, 2009 IP