简体   繁体   English

PHP file_put_contents无法正常工作

[英]PHP file_put_contents not working correctly

I have this code and it does not update the file. 我有此代码,它不会更新文件。 What am I doing wrong here? 我在这里做错了什么?

$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_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);

There's a function date() (and a class DateTime) and maybe you don't know that php is case sensitive. 有一个功能date()(和DateTime类),也许您不知道php区分大小写。

Be sure that: 请确保:

  • the file path is correct 文件路径正确
  • php has permissions to read from and write to the file php有权读取和写入文件

Actually, I found another way to set the code correctly in the template vs. changing the HTML. 实际上,我发现了另一种在模板中正确设置代码而不是更改HTML的方法。 I will use PHP in the template to retrieve it from the database vs. having to change it on the fly. 我将在模板中使用PHP从数据库中检索它,而不必动态更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM