note 43467 rejected from function.fwrite by aidan

From: Date: Thu, 26 Aug 2004 08:55:34 +0000
Subject: note 43467 rejected from function.fwrite by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75301@lists.php.net to get a copy of this message
Note Submitter: chatwithbea at bluewin dot ch ---- People seem to be having trouble removing part of a file. I have created a nifty function that will basically remove anything you want from a file. function remove_from_file($filename,$string_r,$check) { //Open the file $handle=fopen($filename,"r") or die("Error opening file"); //read it $content=fread($handle,filesize($filename)); fclose($handle); //remove what we want to remove $content=str_replace($string_r,"",$content); //delete the old file unlink($filename) or die("Error deleting old file"); //create it $handle=fopen($filename,"a") or die("Error creating file"); //write in it fwrite($handle,$content) or die("Could not write to new file"); fclose($handle); //do we want to reopen the file to check if content has been written? if($check) { $handle=fopen($filename,"r") or die("Could not reopen file for verification"); $result=fread($handle,filesize($filename)); if($result==$content) { return true; } else { return false; } } } Now everytime you want to remove something from a file, use remove_from_file("blah.txt","REMOVEME",false); if you just want to do it, but if you want to check if the modifying ID happen, use if(remove_from_file("blah.txt","REMOVEME",true)){ //blah } setting the last property to true will enable a returning of TRUE or FALSE upon if the file was modified or not.

« previous php.notes (#75301) next »