php fopen 创建文件,但如何更改权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8660778/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:12:12  来源:igfitidea点击:

fopen Creates File, But how to change permissions?

phpfile-permissionsfopenchmodchown

提问by Arjun Bajaj

I am creating a new file using fopen.

我正在使用 fopen 创建一个新文件。

$filename = 'user_data/10.xml';
$openhandle = fopen($filename, 'w+');

Then I check if the file has been created using: file_exists()function.

然后我检查文件是否已使用:file_exists()函数创建。

The problem is: The file is being created with some owner, probably the folder name, but its not me. Also the permissions of the file is only readable by the owner. And since I am not the owner, I can't read the file, or change the permissions.

问题是:文件是由某个所有者创建的,可能是文件夹名称,但不是我。此外,该文件的权限仅为readable by the owner. 由于我不是所有者,因此无法读取文件或更改权限。

But If attempt to change it using:

但是如果尝试使用以下方法更改它:

chown($filename, 'myusername');
chmod($filename, 777);

I tried changing the file owner and permissions using the Terminal using sudo. That worked properly. So I also tried using the functions above with shell_exec()so it runs in root.

我尝试使用终端更改文件所有者和权限sudo。那工作正常。所以我也尝试使用上面的函数,shell_exec()所以它在 root 中运行。

But had no luck.

但没有运气。

Although, I don't have much experience with file permission numbers, the chown command is also not working.

虽然我对文件权限编号没有太多经验,但 chown 命令也不起作用。

So how should I change the owner and permissions of the file so i'm the owner and its readable and writable by my other PHP scripts?

那么我应该如何更改文件的所有者和权限,以便我是所有者并且它可以被我的其他 PHP 脚本读取和写入?

回答by Tim Wickstrom

You should be able to chmod it using only the following line:

您应该能够仅使用以下行对其进行 chmod:

chmod($filename, 0777);

Note the 0 before the 777.

注意 777 之前的 0。

Also do not change the ownership before it has been chmod'ed

在 chmod'ed 之前也不要更改所有权