PHP File_put_contents not working
-
29-06-2021 - |
Question
Check this code:
<?php
$url = 'http://www.example.com/downloads/count.txt';
$hit_count = @file_get_contents($url);
$hit_count++;
@file_put_contents($url, $hit_count);
header('Location: wmwc.zip');
?>
@file_get_contents
is working fine and the header location change to the downloaded file also works, but either the hit_count increase or @file_put_contents
isn't working, because the number with the file doesnt increase by 1. I've set the file permission to 777, but when I try to set the directory permission to 777 also I get a 500 internal server error saying "The server encountered an unexpected condition which prevented it from fulfilling the request."
Solution
You can't write a remote file via http.(If you could do that, every one else could change that file also.)
You need to use the local path.
OTHER TIPS
try changing directory properties
chown www-data:www-data <dirname>
and/or write as follows, if you host on linux
<?php
$var ="hi";
shell_exec('echo "'.$var.'">>log.txt');
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow