User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 422,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,672 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1252 | Replies: 3
Reply
Join Date: Aug 2004
Location: Los Angeles
Posts: 345
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Deleting a line from a file

  #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:
John
Joe
George
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):
$file=fopen("toApprove.txt","r+");
$file2=fopen("temp.txt","w+");
while(!feof($file))
{
	$str = fgets($file);
	if($str != $_GET["file"])
	{
		fwrite($file2,$str);
	}
}
fclose($file);
fclose($file2);

$file=fopen("toApprove.txt","w+");
$file2=fopen("temp.txt","r");
while(!feof($file2))
{
	$str = fgets($file2);
	fwrite($file,$str);
}
fclose($file);
fclose($file2);

Thanks so much for your help!
Last edited by Ghost : Dec 24th, 2007 at 6:05 am.
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Deleting a line from a file

  #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:

$result = $Db->Query("DELETE FROM `table` WHERE `column` = '".mysql_real_escape_string($_GET["file"])
."' 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:

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

or

// retrieve the file into an array where each line is a value
$Array = file('toApprove.txt');
// assuming unique values, lets remove the Array key with value $_GET["file"]
unset($Array[array_search(trim($_GET["file"]), $Array)]);
// explode the array into string with line breaks seperation and put everthing back in the file
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:

if (!file_exists('file_put_contents')) {
// create a simple user function to emulate PHP5's file_put_contents() 
function file_put_contents($file, $contents) {
fwrite($fp = fopen($file), $contents, strlen($contents));
fclose($fp);
}
}

that should work for basic use.
Last edited by digital-ether : Dec 25th, 2007 at 2: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  
Join Date: Aug 2004
Location: Los Angeles
Posts: 345
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Deleting a line from a file

  #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!
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Deleting a line from a file

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC