•
•
•
•
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
![]() |
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:
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):
Thanks so much for your help!
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
$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.
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:
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:
or
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:
that should work for basic use.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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!
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!
•
•
•
•
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- warning:no newline at end of file (C)
- write and delete details on csv file in C++ (C++)
- Reading specific line in a file. / Searching (C++)
- How to find End of file (C++)
- File i/o question (C++)
- host file help (Windows NT / 2000 / XP / 2003)
- Deleting a pointer? (C++)
- Help with logonui.exe file (Windows NT / 2000 / XP / 2003)
- Hijackthis log file & Vbouncer problem - can't remove!!!! (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: Pop up image
- Next Thread: Pls help I need it badly thanks...


Linear Mode