简体   繁体   English

似乎无法在PHP中获得mkdir()的正确权限

[英]Can't seem to get correct permissions for mkdir() in PHP

I have the following simple script to test the mkdir() function in PHP: 我有以下简单的脚本来测试PHP中的mkdir()函数:

<?php
  $id = rand();

  $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/sample_folder/' . $id .'/';
  mkdir(str_replace('//','/',$targetPath), 0755, true);
?>

Ideally, this would create a random folder each time the script is run under my web directory/sample_folder. 理想情况下,每次在我的web目录/ sample_folder下运行脚本时,这将创建一个随机文件夹。 Sample_folder has 755 permissions. Sample_folder具有755个权限。

The issue I face is, I keep running into PHP: mkdir() Permission denied issues. 我面临的问题是,我一直在运行PHP:mkdir()权限被拒绝的问题。 My sample_folder permissions are currently set to chmod 755. 我的sample_folder权限当前设置为chmod 755。

EVERYTHING I have read states not to chmod to 777 so please don't suggest it. 一切我读过的状态不是chmod到777所以请不要建议。

For test purposes, chmod 777 the 'sample_folder' directory addresses the issue but again this poses security issues. 出于测试目的,chmod 777的'sample_folder'目录解决了这个问题,但这又引发了安全问题。 Is there something else I am missing on how to make this work? 关于如何使这项工作还有其他我想念的东西吗?

Of note: my PHP users on the system is "apache"; 值得注意的是:我系统上的PHP用户是“apache”;

I am running PHP 5.3.* and CentOS 5.5 on a Media Temple dedicated virtual server for reference. 我在Media Temple专用虚拟服务器上运行PHP 5.3。*和CentOS 5.5以供参考。 I have also looked through nearly every chmod question on SO and cannot seem to find a solution that matches my issue (with the exception of 777 suggestions). 我还查看了几乎所有关于SO的chmod问题,似乎无法找到符合我的问题的解决方案(除了777个建议)。

edit 编辑

Running ls -la on my server returns: 在我的服务器上运行ls -la返回:

drwxr-xr-x 2 ftphiddenname psacln 4096 Jan 26 11:24 sample_folder drwxr-xr-x 2 ftphiddenname psacln 4096 Jan 26 11:24 sample_folder

final update 最后更新

The answers provided were very helpful. 提供的答案非常有用。 For anybody looking for additional information, I came across this knowledge base article and while it is listed on Media Temple, I blieve the principles apply to any most similar configurations: 对于任何寻找其他信息的人,我都会看到这篇知识库文章,虽然它在Media Temple上列出,但我认为这些原则适用于任何最相似的配置:

(dv):Resolve Apache permission errors (dv):解决Apache权限错误

The reason for this is the script needs write permissions in sample_folder. 原因是脚本需要在sample_folder中写入权限。

I don't know your actual set up, but I'm guessing your script is either running under world permissions or group permission which is 5 (read 4 + execute 1) since your current permissions are 755 (7 for owner, 5 for group and 5 for world). 我不知道你的实际设置,但我猜你的脚本要么是在世界权限下运行,要么是组权限是5(读4 +执行1),因为你当前的权限是755(所有者为7,组为5)和5为世界)。 To write directories into that folder, your script will need write access. 要将目录写入该文件夹,您的脚本将需要写访问权限。 You can set this up more securely than 777 if you have access to chown directories. 如果您有权访问chown目录,则可以比777更安全地设置它。 My advice would be to create a group called 'webgroup' or similar and place your webserver user into that group. 我的建议是创建一个名为“webgroup”或类似组的组,并将您的webserver用户放入该组。 Then, give the group write permissions (770) would be appropriate once you have that set up. 然后,在设置完成后,给予组写权限(770)是合适的。 In case you're a little hazy on how the permissions work, the final setup would be: 如果您对权限的工作方式有点模糊,最终的设置将是:

sample_folder: owned by root, group webgroup, 770 permissions add whatever user apache (or other webserver) is running as to webgroup sample_folder:由root,group webgroup拥有,770权限添加运行webgroup的任何用户apache(或其他web服务器)

EDIT: 编辑:

With more details available in the initial post, this means you would be adding the user 'apache' to webgroup. 在初始帖子中提供了更多详细信息,这意味着您将向webgroup添加用户“apache”。 If you find this too difficult a setup or you do not have full permissions on the server to set this up then using chown as suggested elsewhere to let apache own the directory would work as well. 如果您发现设置太困难,或者您没有在服务器上设置完全权限,那么使用其他地方建议的chown让apache拥有该目录也可以。

For example: chown apache sample_folder 例如:chown apache sample_folder

This will make apache the owner of the folder, giving it access to write permissions (assuming it is still 755) 这将使apache成为该文件夹的所有者,使其能够访问写入权限(假设它仍然是755)

Have you checked the filesystem permissions for the folder under which you are trying to create the directory? 您是否检查了尝试创建目录的文件夹的文件系统权限?

You should cd into the directory and ls -la to see the current ownership and filesystem permissions: 你应该cd到目录和ls -la来查看当前的所有权和文件系统权限:

paulbain@test ~/test $ ls -la
total 8
drwxr-xr-x  2 paulbain apache 4096 Jan 26 17:38 .

In my example above, it's owner Read Write Execute, Group Read and Execute and Everyone Read and Execute. 在上面的例子中,它的所有者是Read Write Execute,Group Read和Execute以及Everyone Read and Execute。

If this was the case and you have your Apache running under a user in the group apache , PHP would not be able to create the directory. 如果是这种情况,并且您的Apache在组apache的用户下运行,则PHP将无法创建该目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM