Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

php

nginx

I am using file_put_contents to create a file. My php process is running in a group with permissions to write to the directory. When file_put_contents is called, however, the resulting file does not have group write permissions (it creates just fine the first time). This means that if I try to overwrite the file it fails because of a lack of permissions.

Is there a way to create the file with group write permissions?

like image 982
Sean Clark Hess Avatar asked Aug 06 '09 16:08

Sean Clark Hess


People also ask

What is the permission value for a file read only for the group owne?

For example, the value 644 sets read and write permissions for owner, and read-only permissions for group and other.

What is File_put_contents?

The file_put_contents() writes data to a file. This function follows these rules when accessing a file: If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename. Create the file if it does not exist. Open the file.


2 Answers

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.

like image 62
danamlund Avatar answered Oct 05 '22 02:10

danamlund


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...

like image 30
Pascal MARTIN Avatar answered Oct 05 '22 01:10

Pascal MARTIN