RedXTech RedXTech - 1 year ago 80
PHP Question

Can't Overwrite Text File - PHP

I have this PHP code on my website, but the isn't working.
What it does, is every time someone clicks the button, the counter goes up one.
I can get the content of the file, and increase it by one, but it can't overwrite the old number, so the count stays at one.

Could I have some help?


$fn = "count.txt";
$orig = file_get_contents($fn);
$plus1 = $orig + 1;

file_put_contents($fn, $plus1);

echo $plus1;


Answer Source

This is most likely a problem with file permissions. You need to make sure that the user your web server is running as has permissions to write to the file. In Ubuntu/Apache, the web server generally runs as www-data. In that case changing the owner of the file to www-data would allow the server to write to the file.

chown www-data count.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download