重写文件问题

michelle0618 2005-03-30 05:55:10
我在一个文件中定义了一些常量。在后台可以修改这些常量。
但是,为何我在后台修改的时候,只要我在原来的基础上增加一个长度,那么就会从文件末尾删除相应的长度,这是为何呢?


大概逻辑如下
$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);
}

这是为什么呢?请高手帮忙!!!
...全文
211 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
michelle0618 2005-03-31
  • 打赏
  • 举报
回复
晕!解决了。不是其他问题。
而是fopen($file,"w+");的时候竟然没有把内容删除完。
在function outputFile($content,$file){
unlink($file);
$handle=fopen($file,"w+");
fwrite($handle,$content);
fclose($handle);
}
里面加上unlink($file);就好了。
让我郁闷了半天。原来是PHP函数问题。
还是谢谢大家:)
michelle0618 2005-03-31
  • 打赏
  • 举报
回复
to:xuzuning(唠叨)
不对啊,如果我把$tail=substr($content,$end);// 改成$tail=substr($content,-$end);那写进文件的就不对了。就乱了
xuzuning 2005-03-31
  • 打赏
  • 举报
回复
你的putConstValue函数有误!

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);//取得尾部需要保存的内容。但是这里用错了参数,应写做$tail=substr($content,-$end);
$const=substr($content,$start,($end-$start));
$const=str_replace($old,$new,$const);
$content=$head.$const.$tail;
return($content);
}
michelle0618 2005-03-31
  • 打赏
  • 举报
回复
可为什么会每修改的内容长度比以前大。而在文件的末尾就减去相应的长度呢?
比如
<?
define("PALACE_NAME5","酒馆");//原市政府
define("PALACE_NAME6","金库");//原银行
.
.
.
define("GOV_LOGO","http://career.wap2008.com/images/gov.png");//原市政府LOGO
define("BANK_LOGO","http://career.wap2008.com/images/bank.png");//原银行LOGO

?>
我在后台对define("PALACE_NAME5","酒馆");//原市政府 里面的内容 “酒馆”根据如上的代码修改为“酒馆1”那么最后的文件就成了
<?
define("PALACE_NAME5","酒馆");//原市政府
define("PALACE_NAME6","金库");//原银行
.
.
.
define("GOV_LOGO","http://career.wap2008.com/images/gov.png");//原市政府LOGO
define("BANK_LOGO","http://career.wap2008.com/images/bank.png");//原银行LOGO


末尾的“?〉”就没了,这是为什么呢?
cokeyang 2005-03-30
  • 打赏
  • 举报
回复
mode 说明
'r' 只读方式打开,将文件指针指向文件头。
'r+' 读写方式打开,将文件指针指向文件头。
'w' 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'w+' 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'a' 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
'a+' 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
michelle0618 2005-03-30
  • 打赏
  • 举报
回复
to:myvicy(在线解决:你的问题我来解决(因为我不会骗人,所以我最)
我用的是fopen($file,"w+");呢
因为我调用的是这个函数来重写
function outputFile($content,$file){
$handle=fopen($file,"w+");
fwrite($handle,$content);
fclose($handle);
}里面用的是“W+”呢。
myvicy 2005-03-30
  • 打赏
  • 举报
回复
$the_file=fopen($file,"w+");
在你要重写的地方这样打开文件

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧