how can I create a file with file_put_contents that has group write permissions? how can I create a file with file_put_contents that has group write permissions? linux linux

how can I create a file with file_put_contents that has group write permissions?


Example 1 (set file-permissions to read-write for owner and group, and read for others):

file_put_contents($filename, $data);chmod($filename, 0664);

Example 2 (make file writable by group without changing other permissions):

file_put_contents($filename, $data);chmod($filename, fileperms($filename) | 16);

Example 3 (make file writable by everyone without changing other permissions):

file_put_contents($filename, $data);chmod($filename, fileperms($filename) | 128 + 16 + 2);

128, 16, 2 are for writable for owner, group and other respectively.


You might what to try setting the umask before calling file_put_contents : it will change the default permissions that will be given to the file when it's created.

The other way (better, according to the documentation) is to use chmod to change the permissions, just after the file has been created.


Well, after re-reading the question, I hope I understood it well...


To open the file and write over contents then you need write permissions to the file. It's important to understand the distinction. To overwrite the entire file you actually need write permissions to the directory.

Use chmod() to set what's appropriate on the file and/or directory if you want to be explicit about it.