By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

open file in read and write mode..w+

P: 5
There is big or problem in open file in read and write mode..
Expand|Select|Wrap|Line Numbers
  1. $file = "data.txt";
  2.  
  3. $fp = fopen($file,"w+");
  4. $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes
  5. echo $line;
  6. fclose($fp);
  7.  
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
Sep 15 '06 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Heya, agphoto.

Try reading the contents of the file using file_get_contents() (PHP: file_get_contents - Manual) and then doing a separate fopen(..., 'w') to overwrite the file contents.
Jan 24 '09 #2

nathj
Expert 100+
P: 938
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
Jan 26 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.