MB34
MB34

Reputation: 4434

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

Upvotes: 0

Views: 222

Answers (2)

MB34
MB34

Reputation: 4434

Actually, I found another way to set the code correctly in the template vs. changing the HTML. I will use PHP in the template to retrieve it from the database vs. having to change it on the fly.

Upvotes: 0

schellingerht
schellingerht

Reputation: 5806

$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.

Be sure that:

  • the file path is correct
  • php has permissions to read from and write to the file

Upvotes: 2

Related Questions