rhyspaterson Posted May 28, 2007 Share Posted May 28, 2007 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>username@domain.com<ema> <s>password</s> <t>user type</t> <p>profile name</p> <cat>000000000000000000000000000000000</cat> </u1> <u2> <n>Username</n> <ema>username@domain.com<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.. Quote Link to comment https://forums.phpfreaks.com/topic/53276-file-re-writing-and-moving-the-file-pointer/ Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/53276-file-re-writing-and-moving-the-file-pointer/#findComment-263280 Share on other sites More sharing options...
utexas_pjm Posted May 28, 2007 Share Posted May 28, 2007 <?php foreach($arrayData as $line){ fwrite($fp, $line); } ?> Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/53276-file-re-writing-and-moving-the-file-pointer/#findComment-263431 Share on other sites More sharing options...
svivian Posted May 28, 2007 Share Posted May 28, 2007 Also if you're storing information in files as XML data, I'm pretty sure there are some nice functions in PHP to make it real easy to read/modify your data. Probably worth looking into. Quote Link to comment https://forums.phpfreaks.com/topic/53276-file-re-writing-and-moving-the-file-pointer/#findComment-263475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.