SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Update File

  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update File

    Hi,

    I know that you can insert data into a empty file easily and add data to a file with data currently. However, is it possible to remove all contents of the file then insert new data? I ask because, if I delete (unlink) the file then create it again using PHP it creates a file that is only deletable using PHP and therefore does not provide useful for most users.

    Can anyone shed some light on this topic?

    -Peter

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, generally in most languages, when you open a file for writing, unless you specify the mode as appending (a), the file pointer will be set to the begining of the file and thus the current contets of the file will be overwritten when you write to the file, and when you close the file the EOF marker will be placed at the end of the new content. So this has the effect of saying - open the file, truncate it to zero length by placing the file pointer at the begining of the file for writing (ie, discard the file's current contents). So for what you want to do, all you need to do is open the file in write mode (w), or read/write mode (w+)
    PHP Code:
    $fp fopen("filename.txt""w"); 

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much for that, I didn't know the difference between 'a' and 'w+' so I used 'a'.

    Thanks again for your help.

    -Peter


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
  •