PHP文件\u put\u内容工作不正常

PHP文件\u put\u内容工作不正常,php,replace,Php,Replace,我有这个代码,它不会更新文件。我做错了什么 $html = file_get_contents('/srv/www/htdocs/code.html'); $lastmon = Date("m", strtotime("first day of previous month")); $html_r = str_replace('<strong>CODE_'.$lastmon.'</strong>', '<strong>CODE_'.Date("m").'<

我有这个代码,它不会更新文件。我做错了什么

$html = file_get_contents('/srv/www/htdocs/code.html');
$lastmon = Date("m", strtotime("first day of previous month"));
$html_r = str_replace('<strong>CODE_'.$lastmon.'</strong>', '<strong>CODE_'.Date("m").'</strong>', $html);
file_put_contents('/srv/www/htdocs/code.html', $html_r);
$html=file\u get\u contents('/srv/www/htdocs/code.html');
$lastmon=日期(“m”,标准时间(“上个月的第一天”);
$html\u r=str\u replace(“CODE.'$lastmon.”,“CODE.'.Date(“m”)。”,$html);
文件内容('/srv/www/htdocs/code.html',$html\r);
$html=文件获取内容('/srv/www/htdocs/code.html');
$lastmon=日期(“m”,标准时间(“上个月的第一天”);
$html\u r=str\u replace(“CODE.'$lastmon.”,“CODE.'.date(“m”)。”,$html);
文件内容('/srv/www/htdocs/code.html',$html\r);
有一个函数date()(和一个类DateTime),可能您不知道php是区分大小写的

确保:

  • 文件路径正确
  • php具有读取和写入文件的权限

实际上,我找到了另一种方法来正确设置模板中的代码,而不是更改HTML。我将在模板中使用PHP从数据库中检索它,而不是动态地更改它。

也许这不是
文件\u put\u内容
不起作用,而是
str\u replace
。函数
Date
不存在,您应该使用
Date
(它可以工作,但正确的拼写是使用
d
)。您应该在每一行之后添加
var\u dump()
,以了解前几行中是否有错误。我认为您正在使用的
Date(“m”)
与调用实际函数的
Date(“m”)
之间存在差异。您的错误日志文件中是否有任何错误?没有错误,即使当我有
Date
Date
以及
Date
时,你也会有错误,所以你的意思是你找不到错误。将其添加到文件
ini\u集的顶部(“日志错误”,1);ini_set(“error_log”,“/tmp/php error.log”)
file\u put\u contents
最常见的故障是该文件夹在Apache系统上没有正确的权限。始终仔细检查
更改为
日期
的权限,oops。还要确保路径正确,并且文件具有777权限。如果不修改文件,还有什么问题?
$html = file_get_contents('/srv/www/htdocs/code.html');
$lastmon = date("m", strtotime("first day of previous month"));
$html_r = str_replace('<strong>CODE_'.$lastmon.'</strong>',     '<strong>CODE_'.date("m").'</strong>', $html);
file_put_contents('/srv/www/htdocs/code.html', $html_r);