open file in read and write mode..w+

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agphoto
    New Member
    • Sep 2006
    • 5

    open file in read and write mode..w+

    There is big or problem in open file in read and write mode..
    Code:
    $file = "data.txt";
    
    $fp = fopen($file,"w+");
    $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes
    echo $line;
    fclose($fp);
    coding is not problem, The problem is file modifier as mention in PHP
    use " w+ " for read the data in file and then overwrite the new data.
    but it is not read the date in file already , while open the file it delete the data in file and then write to it.

    so for read data i had used to open file two times first to read and then with w+ for over write...


    >>and other is "r+" file modifier mention in php read and write, it does but it append the new data to file , which i do not need i need read the data from file and overwrite the new data to file...but w+ is also not worked..


    Please tell the soluion how to solve this..

    Thanks
    Last edited by pbmods; Jan 24 '09, 08:28 AM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, agphoto.

    Try reading the contents of the file using file_get_conten ts() (PHP: file_get_conten ts - Manual) and then doing a separate fopen(..., 'w') to overwrite the file contents.

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Alternatively if you want the file open for read and write but don't want the contents overwritten then try opening the file using the 'a+' mode.

      fopen manual

      This should sort you out, if not then pbmods is the one to follow.

      Cheers
      nathj

      Comment

      Working...