Deleting a line from a file

Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Deleting a line from a file

 
0
  #1
Dec 24th, 2007
Hi,

I'm trying to fully automate my website, and I'm trying to delete a line from a file that matches the line in a given String.
For example, I have a txt file named toApprove.txt containing a list of names such as:
  1. John
  2. Joe
  3. George
  4. Jake
The file is kept in that format. So, if I have a variable holding "Joe", how do I remove the line that says "Joe" from the text file and keep the rest. here's what I tried ($_GET["file"] represents the line I want to remove):
  1. $file=fopen("toApprove.txt","r+");
  2. $file2=fopen("temp.txt","w+");
  3. while(!feof($file))
  4. {
  5. $str = fgets($file);
  6. if($str != $_GET["file"])
  7. {
  8. fwrite($file2,$str);
  9. }
  10. }
  11. fclose($file);
  12. fclose($file2);
  13.  
  14. $file=fopen("toApprove.txt","w+");
  15. $file2=fopen("temp.txt","r");
  16. while(!feof($file2))
  17. {
  18. $str = fgets($file2);
  19. fwrite($file,$str);
  20. }
  21. fclose($file);
  22. fclose($file2);

Thanks so much for your help!
Last edited by Ghost; Dec 24th, 2007 at 7:05 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Deleting a line from a file

 
1
  #2
Dec 25th, 2007
Is there an error you're getting? I don't see any particular problem in the code.

Normally, you'd want to have a database do this job for you. It is much simpler than having to read/write from files.

For example, with a DB it is as simple as:

  1. $result = $Db->Query("DELETE FROM `table` WHERE `column` = '".mysql_real_escape_string($_GET["file"])
  2. ."' LIMIT 1";

The database then does basically what you are doing with the files, on the disk or memory depending on the storage type being used. Databases are very efficient at this though by design.

Your code is very good for file updates as it is very memory efficient. If you are working with small files, you could save time by doing something like:

  1. // retrieve file into a string
  2. $txt = file_get_contents('toApprove.txt');
  3. // replace the line with $_GET["file"], assuming line break is \n
  4. $txt = str_replace(trim($_GET["file"])."\n", '', $txt);
  5. // write the string back to the file
  6. file_put_contents('toApprove.txt', $txt);

or

  1. // retrieve the file into an array where each line is a value
  2. $Array = file('toApprove.txt');
  3. // assuming unique values, lets remove the Array key with value $_GET["file"]
  4. unset($Array[array_search(trim($_GET["file"]), $Array)]);
  5. // explode the array into string with line breaks seperation and put everthing back in the file
  6. file_put_contents('toApprove.txt', implode("\n", $Array));

It is much simpler, however, is memory intensive if you have a large file since you will be saving the whole file contents to a variable, $txt (PHP memory).
file_get_contents() should be more efficient than using fopen() and fgets() for smaller files.. it certainly is more convenient.

file_put_contents() is only supported on PHP5, so you can test for it and your system and create the function if it doesn't exist:

  1. if (!file_exists('file_put_contents')) {
  2. // create a simple user function to emulate PHP5's file_put_contents()
  3. function file_put_contents($file, $contents) {
  4. fwrite($fp = fopen($file), $contents, strlen($contents));
  5. fclose($fp);
  6. }
  7. }

that should work for basic use.
Last edited by digital-ether; Dec 25th, 2007 at 3:50 am. Reason: code formatting
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Deleting a line from a file

 
0
  #3
Dec 25th, 2007
Thanks for your help! There is no database for me to use (I'm using a free web host).
Also, I'm not getting any error message. Instead, it is just not deleting anything from the file. I will try the file_put_contents function, though. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Deleting a line from a file

 
0
  #4
Dec 25th, 2007
Originally Posted by Ghost View Post
Thanks for your help! There is no database for me to use (I'm using a free web host).
Also, I'm not getting any error message. Instead, it is just not deleting anything from the file. I will try the file_put_contents function, though. Thanks!
Yeah, it's usually easier to just put everything from the file in a string or array, and use the string and array functions to manipulate it. It also allows you to dump the whole string after manipulation just to see if it worked.

You may also be interested in a flat file DB. It reads and writes to disk but has similar functionality as a database. Theres a great one posted here a few weeks ago as part of a new CMS. http://www.daniweb.com/forums/post482294.html
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC