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>username1@domain.com<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>username@domain.com<ema>
<s>password</s>
<t>status</t>
<p>profile name</p>
<cat>000000000000000000000000000000000</cat>
</u1>

</users><u2>
<n>username2</n>
<ema>username2@domain.com<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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.