lets say you have a file where you want to insert a string into an existing file
at a specific location. put a key into the file where you want the new
text appear, i.e.:
###INSERTHERE###
then use this
function to insert the new data at this keypoint
01<?php 02 03/*------------------------------------ 04| function insert_string_in_file 05| 06| expects: $file - required [path] to the file you want to work with 07| $insert_point - required [string] which should be acted on 08| $string - required [string] which should be inserted 09| $return - optional [bool] returns message if true, true|false if not 10| $position - optional [string] before|after - default is replace 11| 12| returns: [string] with status message 13| 14|-----------------------------------*/ 15functioninsert_string_in_file($file,$insert_point,$string,$return=FALSE,$position='') 16{ 17// check if file exists 18if(!file_exists($file)) 19{ 20return($return===TRUE?'ERROR - file ('.$file.') does not exist':FALSE); 21} 22// check if file is writeable 23if(!is_writeable($file)) 24{ 25return($return===TRUE?'ERROR - file ('.$file.') is not writeable':FALSE); 26} 27// contruct insertion scheme 28if(empty($position)) 29{ 30// if nothing specified, replace the insertion point with string 31$replacement[$insert_point]=$string; 32}else{ 33if($position=='before') 34{ 35// insert the string before the insertion point 36$replacement[$insert_point]=$string.$insert_point; 37}elseif($position=='after') 38{ 39// insert the string after the insertion point 40$replacement[$insert_point]=$insert_point.$string; 41}else{ 42return($return===TRUE?'ERROR - position ('.$position.') is invalid - 43 use "before" or "after"':FALSE); 44} 45} 46// check filelenght 47$filesize=filesize($file); 48clearstatcache(); 49if($filesize==0) 50{ 51return($return===TRUE?'ERROR - file ('.$file.') is empty':FALSE); 52} 53// open file for read and write access 54if(!$handle=fopen($file,'r+')) 55{ 56return($return===TRUE?'ERROR - cannot open file ('.$file.')':FALSE); 57}else{ 58$old_contents=fread($handle,$filesize); 59} 60// insert string at the insertion point 61if(!strpos($old_contents,$insert_point)) 62{ 63fclose($handle); 64return($return===TRUE?'ERROR - insertion point ('.$insert_point.') not found':FALSE); 65}else{ 66ftruncate($handle,0); 67$new_contents=strtr($old_contents,$replacement); 68} 69// write new content to file 70if(fwrite($handle,$new_contents)===FALSE) 71{ 72fclose($handle); 73return($return===TRUE?'ERROR - cannot write to file ('.$file.')':FALSE); 74} 75fclose($handle); 76return($return===TRUE?'SUCCESS - your string was inserted':TRUE); 77} 78 79// use like 80$the_file='path/to/file.txt'; 81$insert_key='###INSERTHERE###'; 82 83$new_data=' your text to insert'; 84 85// insert string before insertion point and get verbose messages 86echoinsert_string_in_file($the_file,$insert_key,$new_data,TRUE,'before').'<br />'; 87 88$new_data=' should be here'; 89 90// simply replace insertion point 91if(insert_string_in_file($the_file,$insert_key,$new_data)) 92{ 93echo$insert_key.' was replaced by '.$new_data.' in the file '.$the_file.'.'; 94}else{ 95echo'insertion failed<br />'; 96} 97 98?>