PHP mkdir 和 apache 所有权

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

PHP mkdir and apache ownership

phpapachemkdirownerchown

提问by Bill H

Is there a way to set php running under apache to create folders with the folder owned by the owner of the program that creates it instead of being owned by apache?

有没有办法设置在 apache 下运行的 php 来创建文件夹,该文件夹由创建它的程序的所有者拥有,而不是由 apache 拥有?

Using word press it creates new folders to upload into but these are owned by apache.apache and not by the site that they are running in. This also happens using ostickets. For now we have to SSH into the server and chmod the folder, but it would seem there would be a setting somewhere to override the ownership outside of any program that does it.

使用 word press 它会创建要上传到的新文件夹,但这些文件夹由 apache.apache 拥有,而不是由它们运行的​​站点拥有。使用 ostickets 也会发生这种情况。现在我们必须通过 SSH 连接到服务器并 chmod 文件夹,但似乎在某处会有一个设置来覆盖任何执行此操作的程序之外的所有权。

采纳答案by Sanhe

Safe_modeis turn on on your server. The function mkdir()creates folder with owner ("apache", "none", ..) that different of the current script owner. And scripts couldn't upload (move, copy) files into that folder with another owner (that is not like current script owner).

Safe_mode在您的服务器上打开。该函数mkdir()创建文件夹,其所有者(“apache”、“none”、..)与当前脚本所有者不同。并且脚本无法将文件上传(移动、复制)到另一个所有者(与当前脚本所有者不同)的文件夹中。

Disable safe_modeand that would be work.

禁用safe_mode,这将是工作。

See http://php.net/manual/en/features.safe-mode.phpfor details.

有关详细信息,请参阅http://php.net/manual/en/features.safe-mode.php

P.S. With enable safe_modeyou can't use chmod()function in php.

PS 启用后,safe_mode您不能chmod()在 php 中使用函数。

回答by Progman

Another way is to put the apache user and the "customer users" in a new group. Additional the directory should use the sticky bit SGIDso each new file got the group assignment to this new group. This way the webserver and the "customer users" can work with the files without any problems

另一种方法是将 apache 用户和“客户用户”放在一个新组中。此外,目录应该使用粘滞位,SGID以便每个新文件都将组分配给这个新组。这样网络服务器和“客户用户”可以毫无问题地处理文件

[17:57] progman@proglap /tmp/test $ ls -al /tmp/test
total 9
drwxrwsr-x  2 root users   48 Apr  1 17:55 .
drwxrwxrwt 36 root root  9264 Apr  1 17:53 ..

As you see the directory got the stick bit SGIDand the owner is the "users" group in which I (progman) am. No if another user adds a file the group automatically get set to this group

正如您所看到的,目录得到了控制SGID,所有者是我 ( progman)所在的“用户”组。否,如果另一个用户添加了一个文件,该组会自动设置为该组

[17:55] proglap ~ # touch /tmp/test/x

This is executed from root. Now we get:

这是从 root 执行的。现在我们得到:

[17:57] progman@proglap /tmp/test $ ls -la /tmp/test
total 9
drwxrwsr-x  2 root users   72 Apr  1 17:59 .
drwxrwxrwt 36 root root  9264 Apr  1 17:53 ..
-rw-r--r--  1 root users    0 Apr  1 17:59 x

As you see the added file is from root, but the group is set to usersand this way Ican remove it

如您所见,添加的文件来自根目录,但组设置为users这样可以将其删除

[18:00] progman@proglap /tmp/test $ rm x
rm: remove write-protected regular empty file `x'? y
[18:01] progman@proglap /tmp/test $ ls -la /tmp/test
total 9
drwxrwsr-x  2 root users   48 Apr  1 18:01 .
drwxrwxrwt 36 root root  9264 Apr  1 17:53 ..

Keep in mind that you still need to change the chmodif you want to editthe file as rw-r--r--is just group read access. But changing the chmod, maybe even working with umask, is better than dealing with root-access and using chown.

请记住,你仍然需要改变chmod,如果你想编辑的文件rw-r--r--仅仅是组读访问。但是更改chmod,甚至可能使用umask,比处理 root 访问和使用chown.

回答by Marc B

Not directly, no. You can't "give away" ownership of a file to another user, unless you're root. You could investigate using the "AssignUserID" apache directive to force that particular vhost to run as a particular user/group. With that Apache/PHP would create any files with the appropriate ownership

不直接,不。除非您是 root,否则您不能将文件的所有权“放弃”给另一个用户。您可以调查使用“ AssignUserID”apache 指令强制该特定虚拟主机作为特定用户/组运行。使用该 Apache/PHP 将创建具有适当所有权的任何文件

回答by Phill Pafford

Check out PHP chown()function

查看 PHP chown()函数