Changing file permission

118 views Asked by At

I'm trying to save an image from a remote location to a local location. Currently when I save the file it's saving correctly, with the correct permissions (0755) with with the wrong owner / group. It's currently saving as group 'nobody' and owner 'nobody'. I need to save this as the username instead.

Here is my code that saves the image:

file_put_contents($filename, $content);

chgrp($filename, 'username');
chown($filename, 'username');
chmod($filename, 0755);
2

There are 2 answers

2
Marc B On BEST ANSWER

You cannot chown a file to another user, unless you're running as root. This is a security measure. A hostile user could trivially create/chown a bunch of files to a different user and consume that user's quota.

0
xeo On

have a look at the sticky bit: (http://computernetworkingnotes.com/managing-file-system-security/sticky-bit.html). this allows any new files / folders created in the folder to have a permissions that match the folder (automagically).

also have a look at using groups vs users for setting these permissions. I tend to use chown apache:web for my folders these days. then i can put my ftpuser and other users in the web group (or remove them) to allow editing during periods of changes.