如何使用具有组写权限的file_put_contents创建文件?

时间:2022-05-14 09:12:18

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.

我正在使用file_put_contents来创建一个文件。我的php进程在一个具有写入目录权限的组中运行。但是,当调用file_put_contents时,生成的文件没有组写权限(第一次创建就好了)。这意味着如果我尝试覆盖该文件,则由于缺少权限而失败。

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

有没有办法用组写权限创建文件?

3 个解决方案

#1


21  

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.

您可能在调用file_put_contents之前尝试设置umask:它将更改创建文件时将提供给文件的默认权限。

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

另一种方式(更好的是,根据文档)是在创建文件之后使用chmod来更改权限。


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

那么,在重新阅读这个问题之后,我希望我理解得很好......

#2


23  

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

示例1(将文件权限设置为所有者和组的读写,并为其他人读取):

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

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

示例2(使文件可以按组写入而不更改其他权限):

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

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

示例3(使所有人都可以写入文件而不更改其他权限):

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

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

128,16,2分别用于所有者,团体和其他人的可写。

#3


2  

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.

如果要明确说明文件和/或目录,请使用chmod()设置适当的选项。

#1


21  

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.

您可能在调用file_put_contents之前尝试设置umask:它将更改创建文件时将提供给文件的默认权限。

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

另一种方式(更好的是,根据文档)是在创建文件之后使用chmod来更改权限。


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

那么,在重新阅读这个问题之后,我希望我理解得很好......

#2


23  

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

示例1(将文件权限设置为所有者和组的读写,并为其他人读取):

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

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

示例2(使文件可以按组写入而不更改其他权限):

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

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

示例3(使所有人都可以写入文件而不更改其他权限):

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

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

128,16,2分别用于所有者,团体和其他人的可写。

#3


2  

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.

如果要明确说明文件和/或目录,请使用chmod()设置适当的选项。