Jump to content

[SOLVED] fopen and a+


rhyspaterson

Recommended Posts

Hey guys.

 

I have a file that i open with fopen a+, meaning when i open the file it places the pointer at the end of the file and writes from there. I then write an array to the file.

 

My only problem is, when i place the pointer at the end of the file it looks like this.

 

<bt>600</bt>
<iregno>123456789</iregno>

<users>
<un>1</un>

<u1>
<n>username1</n>
<ema>[email protected]<ema>
<s>password</s>
<t>status</t>
<p>profile name</p>
<cat>000000000000000000000000000000000</cat>
</u1>

</users><<<<< ------ pointer is here

 

and then when i append my data, file looks like this

 

<bt>600</bt>
<iregno>123456789</iregno>

<users>
<un>2</un>

<u1>
<n>username</n>
<ema>[email protected]<ema>
<s>password</s>
<t>status</t>
<p>profile name</p>
<cat>000000000000000000000000000000000</cat>
</u1>

</users><u2>
<n>username2</n>
<ema>[email protected]<ema>
<s>password</s>
<t>status</t>
<p>profile name</p>
<cat>000000000000000000000000000000000</cat>
</u2>

</users>

 

the bad part being

 

</users><u2>

 

i would like to overwrite the last line and continue adding my data. Is this possible in any way?

 

Thanks so much,

/Rhys

Link to comment
https://forums.phpfreaks.com/topic/53411-solved-fopen-and-a/
Share on other sites

it looks like fseek is the way to go, but i am just not quite sure how to implement it..

 

/edit figured it out.

  $fp = fopen($local_file, "r+") or die ("Can't open user file");
  fseek($fp, -8, SEEK_END);
  foreach($userArray as $val) {
	fwrite($fp, $val);
  }
  fclose($fp);

 

thanks lads.

Link to comment
https://forums.phpfreaks.com/topic/53411-solved-fopen-and-a/#findComment-263906
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.