I have a PHP script that copies a file from one location to another, the script looks like this:
$success = copy('/tmp/source_test.txt', '/tmp/target_test.txt');
if (!$success) {
echo "Copy failed: " . error_get_last()['message'] . "nn";
} else {
echo "Copied OKnn";
}
Both source_test.txt and target_test.txt exist and have the owner and permission set as follows:
- source_test.txt (Owner: root:root; chmod: 644)
- target_test.txt (Owner: php:wwwuser; chmod: 644)
I run the script on 2 server instances as ‘root’ by calling php /path/to/script.php, both instances are on Alma linux.
- On the first instance, it works fine, the target_test.txt gets overwritten.
- On the second instance, it fails with this message “Failed to open stream: Permission denied”.
It looks like the second is missing something or configuration that the PHP copy() function, even though being executed as root, does not have the permission to overwrite the target file.
Anyone experienced this problem before? Any advice is appreciated.