1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with overwriting a file

Discussion in 'PHP' started by piniyini, Apr 2, 2005.

  1. #1
    Hi guys,

    After checking out php.net/fwrite I managed to write to a file on my website, here is the code I used:

    $filename = 'test.txt';
    $somecontent = "hello my name ist habe piniyini";
    
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    
       // In our example we're opening $filename in append mode.
       // The file pointer is at the bottom of the file hence 
       // that's where $somecontent will go when we fwrite() it.
       if (!$handle = fopen($filename, 'a')) {
             echo "Cannot open file ($filename)";
             exit;
       }
    
       // Write $somecontent to our opened file.
       if (fwrite($handle, $somecontent) === FALSE) {
           echo "Cannot write to file ($filename)";
          exit;
       }
       
       echo "Success, wrote ($somecontent) to file ($filename)";
       
       fclose($handle);
    
    } else {
       echo "The file $filename is not writable";
    }
    PHP:
    The text file is here

    What I want to do is overwrite the text in that file, how do I do this?

    Ta
     
    piniyini, Apr 2, 2005 IP
  2. piniyini

    piniyini Well-Known Member

    Messages:
    514
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    170
    #2
    it alwright guys I got it covered:

    if (!$handle = fopen($filename, 'w))
    PHP:
    note the change from 'a' to 'w'

    :rolleyes:
     
    piniyini, Apr 2, 2005 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $f = fopen($filename, 'w');
    PHP:
    Attention to detail makes a good programmer :)
     
    exam, Apr 2, 2005 IP