重写文件问题
我在一个文件中定义了一些常量。在后台可以修改这些常量。
但是,为何我在后台修改的时候,只要我在原来的基础上增加一个长度,那么就会从文件末尾删除相应的长度,这是为何呢?
大概逻辑如下
$const_file=FILE_PATH."/$filename";//常量文件
$const=inputFile($const_file);
$GAME_INDEX_INTRO=getConstValue("GAME_INDEX_INTRO",$const);//首页介绍
/*(原市政府)提示信息*/
$GOV_INTRO=getConstValue("GOV_INTRO",$const);
$RE_GAME_INDEX_INTRO=$_REQUEST["GAME_INDEX_INTRO"];//首页介绍
/*(原市政府)提示信息*/
$RE_GOV_INTRO=$_REQUEST["GOV_INTRO"];
/*首页介绍*/
outputFile(putConstValue("GAME_INDEX_INTRO",$GAME_INDEX_INTRO,$RE_GAME_INDEX_INTRO,$const),$const_file);
/*(原市政府)提示信息*/
$const=inputFile($const_file);
outputFile(putConstValue("GOV_INTRO",$GOV_INTRO,$RE_GOV_INTRO,$const),$const_file);
function inputFile($file){
$handle=fopen($file,"r");
$content=fread($handle,filesize($file));
fclose($handle);
return($content);
}
function outputFile($content,$file){
$handle=fopen($file,"w+");
fwrite($handle,$content);
fclose($handle);
}
function getConstValue($const_name,$content){
$const_name="define(\"".$const_name."\",";
$start=strpos($content,$const_name)+strlen($const_name);
$end=strpos($content,");",$start);
$ret=substr($content,$start,($end-$start));
$ret=str_replace("\"","",$ret);
$ret=str_replace("\\r","",$ret);
$ret=str_replace("\\n","",$ret);
$ret=str_replace("<br/>","",$ret);
return($ret);
}
function putConstValue($const_name,$old,$new,$content){
$const_name="define(\"".$const_name."\",";
$start=strpos($content,$const_name)+strlen($const_name);
$end=strpos($content,");",$start);
$head=substr($content,0,$start);
$tail=substr($content,$end);
$const=substr($content,$start,($end-$start));
$const=str_replace($old,$new,$const);
$content=$head.$const.$tail;
return($content);
}
这是为什么呢?请高手帮忙!!!