php 如何使用具有组写入权限的 file_put_contents 创建文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1240034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I create a file with file_put_contents that has group write permissions?
提问by Sean Clark Hess
I am using file_put_contentsto create a file. My php process is running in a group with permissions to write to the directory. When file_put_contentsis 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?
有没有办法创建具有组写入权限的文件?
采纳答案by Pascal MARTIN
You might what to try setting the umaskbefore calling file_put_contents: it will change the default permissions that will be given to the file when it's created.
您可能会尝试umask在调用之前设置什么file_put_contents:它会更改创建文件时授予文件的默认权限。
The other way (better, according to the documentation) is to use chmodto change the permissions, just after the file has been created.
另一种方法(更好,根据文档)是chmod在创建文件后使用更改权限。
Well, after re-reading the question, I hope I understood it well...
好吧,在重新阅读问题后,我希望我能理解它......
回答by danamlund
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分别为owner、group、other可写。
回答by cletus
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()如果您想明确说明文件和/或目录,请使用它来设置适当的内容。

