AbydosGater Posted January 6, 2007 Share Posted January 6, 2007 Hi,I have decided that i would like to do the next section of my site.. a simming/roleplay section using .txt or .dat files all the same to hold messages and forum posts..But i was wondering if i do that and use a newline (\n) for each post.. is there any way to edit or delete that post/message easily?With an sql database you can just use an UPDATE or DELETE but how would you go about it using a text file?Thanks-Andy Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/ Share on other sites More sharing options...
AbydosGater Posted February 1, 2007 Author Share Posted February 1, 2007 Bump..I know i need to loop through each line finding the one to edit but i cant seam to find any examples to get the idea for the code.#Andy Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174624 Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 so you are using a txt file, and you want to edit a specified line, like:[code]line1.....line2.....line3...........[/code]and you just want to edit line2 and keep the rest the same?Ted Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174635 Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 $id is the line number you want to edit.[code]// open file$file = "test.txt";// get contents of file, line by line, and put into an array$data = file($file);$data[$id] = NULL;// Open file for writing$handle = fopen($file, "w");// for each line...foreach ($data as $val) {// write the line to the file$write = fwrite($handle, $val);} // end foreach// close filefclose($handle);[/code]Ted Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174637 Share on other sites More sharing options...
obsidian Posted February 1, 2007 Share Posted February 1, 2007 You would need some way to reference each line of the code (id, username, timestamp, etc). Once you have a reference point, you can easily cycle through all the data in the file and pull the one you're after. Where you have to be careful is [i]editing[/i] a very large file. If the file is small enough to pull the whole thing into a variable or array, you can easily loop through the records, edit the one you need and spit them all back out into the file again, but if you have a text file that is many, many MB, you'll start having a problem with filesize if you try to pull all the rows into a single variable. At that point, you'll need to read line by line, writing the results to a temp file. When you reach the line that matches what you're after, edit the line, write it to the temp file, and proceed to write the rest of the text file into your temp file. Once you are done, your temp file holds the corrected data, so you overwrite the original with the temp file (or simply delete the original and rename the temp file).The biggest issue I've run into with this method is that, when you reach a level with [b]large[/b] files to work with (in some cases 80-100MB or more), if you have multiple users reading and editing the same file, you run the risk of data loss.Hope this helps some. Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174638 Share on other sites More sharing options...
AbydosGater Posted February 1, 2007 Author Share Posted February 1, 2007 Yeah say i have a users database in .txtuserid||username||password1||admin||encryptedpass2||member1||idunno3||mem2||forgetitAnd i just want to edit member1..Of course id never use it for users without keeping the file save in a directory that is .htacces protected and all the information encrypted..~Andy Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174643 Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 [code]// open file $handle = fopen("test.txt", "r+b");// get contents of file, line by line, and put into an arraywhile (!feof($handle)) { $list[] = fgets($handle, 4096);} // end while// assuming line nodes are sperated by dots..// explode the 2nd line of the file$nodes = explode('||',$list[1]);// change the 2nd node to something else$nodes[1] = "test";//*****************change this to whatever you want, second variable of the second line// make it back to a string put it back into the list$list[$id] = implode('||',$nodes);// put the pointer back to the beginningrewind($handle);// for each line...foreach ($list as $val) { // write the line to the file$write = fwrite($handle, $val);} // end foreach// close filefclose($handle);[/code] Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174645 Share on other sites More sharing options...
obsidian Posted February 1, 2007 Share Posted February 1, 2007 Again, while ted_chou12's code will definitely do what you're after, be very cautious assuming that your entire user list with all their information will fit within a single variable. You'll be much better off in the long run coming up with a simplistic file caching system and write the files [b]as you read them[/b] and alter line by line instead of trying to load the whole file into one variable. Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174649 Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 yeah, i agree with obsidian, if is something really important, then saving it in mysql db ensures the security as well as the quickness... ;DTed Link to comment https://forums.phpfreaks.com/topic/33092-editing-a-txt-file-only-one-line/#findComment-174652 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.