| | |
Deleting a line from a file
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
PHP Syntax (Toggle Plain Text)
John Joe George Jake
PHP Syntax (Toggle Plain Text)
$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 7: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:
PHP Syntax (Toggle Plain Text)
$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:
PHP Syntax (Toggle Plain Text)
// 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
PHP Syntax (Toggle Plain Text)
// 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:
PHP Syntax (Toggle Plain Text)
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 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!
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!
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!
![]() |
Similar Threads
- 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)
- Deleting a pointer? (C++)
- Help with logonui.exe file (Windows NT / 2000 / XP)
- Hijackthis log file & Vbouncer problem - can't remove!!!! (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: Torrent website Scripts
- Next Thread: Pls help I need it badly thanks...
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube






