Textfile Delete Row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tylerrowsell
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 6:42 pm

Textfile Delete Row

Post by tylerrowsell »

Im making like a guest book and each new entry i saved on a new row in a text file is there a way i can choose a specific row and delete it?

Thanks
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: Textfile Delete Row

Post by anand »

tylerrowsell wrote:Im making like a guest book and each new entry i saved on a new row in a text file is there a way i can choose a specific row and delete it?

Thanks
You are using any database.. like MySQL ?
tylerrowsell
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 6:42 pm

Re: Textfile Delete Row

Post by tylerrowsell »

nope im just using a txt file no mysql or anything... like i know how to select a specfic row but i don't know how to delete it
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Textfile Delete Row

Post by onion2k »

You'll need to open the file, put everything you want up to the row you want to delete into an array, skip the row to be deleted, then read the rest into the array, and finally write the whole lot back to the text file. It'll be slow and horrible. Use a database.
tylerrowsell
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 6:42 pm

Re: Textfile Delete Row

Post by tylerrowsell »

i would use a asp because i know that language better but our server does not support that and it does not support databases

Thanks though

it should not be to hard to do that because it is only the first row that needs to be deleted
tylerrowsell
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 6:42 pm

Re: Textfile Delete Row

Post by tylerrowsell »

That is excellent much better an easier then what i had put in... thanks a lot !!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Textfile Delete Row

Post by Darhazer »

No need to read the whole file... Since this is my 100th post, here is the proper solution (I didn't find anyone written this on Google). Not perfect, there is a bug list the last line, but's here 1:30 AM

Code: Select all

 
<?php
$file = fopen('test.txt', 'r+');
$lineToDelete = 3;
$counter = 1;
 
while ($counter < $lineToDelete) {
   fgets($file); // skip
   $counter++;
   
}
$write = true;
 
$position = ftell($file);
while ($line = fgets($file)) {
   $newLine = fgets($file);
   if ($newLine == false) break;
   fseek($file, $position, SEEK_SET);
   fwrite($file, $newLine);
   $position = ftell($file);
}
fclose($file);
echo 'Done';
?>
 
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Textfile Delete Row

Post by Darhazer »

Actually in the code above, in the second while fread() with fixed size of buffer should be used instead of fgets(). Later this week I will edit the post with the correct code, if anyone don't fix this before me :mrgreen:

Edit:
This one works correctly, still I feel some refactoring needed:

Code: Select all

<?php
$size = filesize('test.txt');
$file = fopen('test.txt', 'r+');
$lineToDelete = 3;
$counter = 1;
 
while ($counter < $lineToDelete) {
   fgets($file); // skip
   $counter++;
   
}
 
$position = ftell($file);
$lineToRemove = fgets($file);
$bufferSize = strlen($lineToRemove);
while ($newLine = fread($file, $bufferSize)) {
   fseek($file, $position, SEEK_SET);
   fwrite($file, $newLine);
   $position = ftell($file);
   fseek($file, $bufferSize, SEEK_CUR);
   
}
ftruncate($file, $size - $bufferSize);
echo 'Done';
fclose($file);
?>
tylerrowsell
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 6:42 pm

Re: Textfile Delete Row

Post by tylerrowsell »

to do this what would need to be changed just the lineToDelete so if that changes to 4 it will delete line 4 do i need to change counter?
Darhazer wrote:Actually in the code above, in the second while fread() with fixed size of buffer should be used instead of fgets(). Later this week I will edit the post with the correct code, if anyone don't fix this before me :mrgreen:

Edit:
This one works correctly, still I feel some refactoring needed:

Code: Select all

<?php
$size = filesize('test.txt');
$file = fopen('test.txt', 'r+');
$lineToDelete = 3;
$counter = 1;
 
while ($counter < $lineToDelete) {
   fgets($file); // skip
   $counter++;
   
}
 
$position = ftell($file);
$lineToRemove = fgets($file);
$bufferSize = strlen($lineToRemove);
while ($newLine = fread($file, $bufferSize)) {
   fseek($file, $position, SEEK_SET);
   fwrite($file, $newLine);
   $position = ftell($file);
   fseek($file, $bufferSize, SEEK_CUR);
   
}
ftruncate($file, $size - $bufferSize);
echo 'Done';
fclose($file);
?>
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Textfile Delete Row

Post by Darhazer »

Only $lineToDelete should be changed.
Post Reply