I have a case that require I to write some string to the file that only root
can do that. I was try some solutions base on this post but none of them works.
CASE:
When user was successfully done, he automatically access writefile.php
which contain script that can write to the file ( tes.txt
) that only root
have access.
I used visudo
and added this to my sudoers
file:
username ALL=(root) NOPASSWD: /var/www/html/myprogram/writefile.php
not works!
username ALL=(ALL) NOPASSWD: /var/www/html/myprogram/writefile.php
not works!
My simple code in writefile.php
<?php
echo file_put_contents("tes.txt","Hello World. Testing!");
?>
Result:
Warning: file_put_contents(tes.txt): failed to open stream: Permission denied