Jump to content

File re-writing and moving the file pointer


rhyspaterson

Recommended Posts

Hey guys,

 

Pretty new to the whole file writing functions within PHP and i was looking for some help. Basically i have a text file that looks like this:

 

<bt>600</bt>
<iregno>123123</iregno>

<users>
<un>2</un>

<u1>
<n>Username</n>
<ema>[email protected]<ema>
<s>password</s>
<t>user type</t>
<p>profile name</p>
<cat>000000000000000000000000000000000</cat>
</u1>

<u2>
<n>Username</n>
<ema>[email protected]<ema>
<s>password</s>
<t>user type</t>
<p>profile name</p>
<cat>000000000000000000000000000000000</cat>
</u2>

and so on

</users>

 

Now i run this big user edit function where im reading in their details and displaying them in forms and so on. They then edit the form fields and i read them back in using $POST. That's the easy part. What i then want to do is re-write a section of my text file.

 

I have a variable that tells me what user i need to re-write the details for, i also have all the new details that i need to replace the old ones with. So, in logical terms, what i need to do is move my file pointer to that section of the file:

 

read in $userToEdit variable -> output of variable is <u1> -> move file pointer to next line (<n>username</n>) and let me rewrite the next 6 lines therefore leaving the pointer at </u1>

 

I hope that makes sense. Just basically want to rewrite the text that is there, not append/add.

 

Any suggestions?

Cheers..

Ok i figured out that i cant just open the file, store it as an array, and then change the variables in the array. All i need to do now is to write the array back to the file. But obviously:

 

fwrite($fp, $arrayData)

 

will not work. Any pointers on how to write an array and all of its data to a text file?

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.