I have problem where i need to set the file owner permission to different user in the system via a php script
so i do this via this following command where 1002 is the user id of the system.
file_put_contents($filename, $content);
system("chown 1002 " . $filename . "");
however i get this error in productions server only (test server it works fine)
chown: changing ownership of `/var/spool/asterisk/06h12m7.call':
Operation not permitted
Since you tagged this question as
LinuxI’m assuming that you useApacheserver. In production servers theApacheprocess, which owns all php processes, are usually executed by theapache useror other user that is not theroot user.Bearing that in mind, what you are trying to do is using the
chownfunction, (which will be executed as apache user) to change the owner of a file that you don’t own. (Yes, you can only change owners to the files you own).You see, quoting the php manual, the chown function attempts to change the owner:
In production servers usually you are running as in user directory mode, which means you are bound to the files that are in your home directory, something like
/home/yourusername/public_htmland as such, files inside the/vardirectory are simply out of your reach (They are usually owned by root) and that’s why you can’t chown.I hope it helped. Cheers!