Jump to content

Editing a txt file.. only one line


AbydosGater

Recommended Posts

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

  • 4 weeks later...
$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 file
fclose($handle);
[/code]
Ted
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.
Yeah say i have a users database in .txt

userid||username||password
1||admin||encryptedpass
2||member1||idunno
3||mem2||forgetit

And 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
[code]
// open file
$handle = fopen("test.txt", "r+b");

// get contents of file, line by line, and put into an array
while (!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 beginning
rewind($handle);

// for each line...
foreach ($list as $val) {
// write the line to the file
$write = fwrite($handle, $val);
} // end foreach

// close file
fclose($handle);
[/code]
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.

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.