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
Linux
I'm assuming that you useApache
server. In production servers theApache
process, which owns all php processes, are usually executed by theapache user
or other user that is not theroot user
.Bearing that in mind, what you are trying to do is using the
chown
function, (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_html
and as such, files inside the/var
directory 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!
You are only allowed to change the owner of a file if you are the root user (or running with root privileges).
Most likely on your test server the apache process running the php script already owns the file, or you are running it with root privileges, and this isn't the case on your production server. I would definitely not recommend running in a production environment with root privileges.
Some work arounds that may help depending on the situation, you can change the "group" setting of the file using the
chgrp
function. This is permitted by anyone with write privileges on the file. You can also make the file writeable to anyone using thechmod
function, although this can be dangerous. Links to both functions are listed below: