如何授予 PHP 对目录的写访问权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2900690/
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 do I give PHP write access to a directory?
提问by Leagsaidh Gordon
I'm trying to use PHP to create a file, but it isn't working. I am assuming this is because it doesn't have write access (it's always been the problem before). I tried to test if this was the problem by making the folder chmod 0777, but that just ended up making every script in that directory return a 500 error message until I changed it back. How do I give PHP write access to my file system so it can a create a file?
我正在尝试使用 PHP 创建一个文件,但它不起作用。我假设这是因为它没有写访问权限(以前一直是这个问题)。我试图通过创建文件夹 chmod 0777 来测试这是否是问题,但这最终使该目录中的每个脚本都返回 500 错误消息,直到我将其更改回来。我如何授予 PHP 对我的文件系统的写访问权限,以便它可以创建文件?
Edit: It is hosted on Hostgator shared hosting using Apache.
编辑:它托管在使用 Apache 的 Hostgator 共享主机上。
Edit 2: Someone asked for the code: The code is a GD image script. I know the rest of it works as previously I was creating the image every ime it was called. Now I am trying to create them when new text is added and save them to a folder. The write line I have is: imagejpeg(null,$file,85);
编辑 2:有人要代码:代码是 GD 图像脚本。我知道它的其余部分像以前一样工作,因为我每次调用它时都会创建图像。现在我试图在添加新文本时创建它们并将它们保存到文件夹中。我的写行是: imagejpeg(null,$file,85);
I also created a test file to check if it was just a broken script (mainly copied from tizag): http://gearboxshow.info/rkr/lesig.jpg/testfile.txt(I don't know if/how to post the code here properly. Here is the contents of the PHP script, minus PHP tags.)
我还创建了一个测试文件来检查它是否只是一个损坏的脚本(主要是从 tizag 复制的):http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (我不知道是否/如何发布这里的代码正确。这里是 PHP 脚本的内容,减去 PHP 标签。)
It returns 13,13,1 (separate lines), so it looks as if it thinks it wrote something, but the testfile.txt is blank (I uploaded a blank one), or non-existent (if I delete it).
它返回 13,13,1(单独的行),所以看起来好像它写了一些东西,但是 testfile.txt 是空白的(我上传了一个空白的),或者不存在(如果我删除它)。
Edit 3: The server runs CentOS.
编辑 3:服务器运行 CentOS。
采纳答案by favo
An easy way is to let PHP create the directory itself in the first place.
一个简单的方法是让 PHP 自己首先创建目录。
<?php
$dir = 'myDir';
// create new directory with 744 permissions if it does not exist yet
// owner will be the user/group the PHP script is run under
if ( !file_exists($dir) ) {
mkdir ($dir, 0744);
}
file_put_contents ($dir.'/test.txt', 'Hello File');
This saves you the hassle with permissions.
这可以为您省去权限方面的麻烦。
回答by Thomas Winsnes
Set the owner of the directory to the user running apache. Often nobody on linux
将目录的所有者设置为运行 apache 的用户。linux 上经常没人
chown nobody:nobody <dirname>
This way your folder will not be world writable, but still writable for apache :)
这样你的文件夹就不会是世界可写的,但仍然可以为 apache 写:)
回答by Grant Miller
Simple 3-Step Solution
简单的三步解决方案
Abstract: You need to set the owner of the directory to the user that PHP uses (web server user).
摘要:您需要将目录的所有者设置为 PHP 使用的用户(web 服务器用户)。
Step 1: Determine PHP User
第 1 步:确定 PHP 用户
Create a PHP file containing the following:
创建一个包含以下内容的 PHP 文件:
<?php echo `whoami`; ?>
Upload it to your web server. The output should be similar to the following:
将其上传到您的 Web 服务器。输出应类似于以下内容:
www-data
Therefore, the PHP user is www-data.
因此,PHP 用户是www-data.
Step 2: Determine Owner of Directory
步骤 2:确定目录的所有者
Next, check the details of the web directory via the command line:
接下来,通过命令行查看 web 目录的详细信息:
ls -dl /var/www/example.com/public_html/example-folder
The result should be similar to the following:
结果应类似于以下内容:
drwxrwxr-x 2 exampleuser1 exampleuser2 4096 Mar 29 16:34 example-folder
Therefore, the owner of the directory is exampleuser1.
因此,目录的所有者是exampleuser1.
Step 3: Change Directory Owner to PHP User
第 3 步:将目录所有者更改为 PHP 用户
Afterwards, change the owner of the web directory to the PHP user:
之后,将 web 目录的所有者更改为 PHP 用户:
sudo chown -R www-data /var/www/example.com/public_html/example-folder
Verify that the owner of the web directory has been changed:
验证 Web 目录的所有者是否已更改:
ls -dl /var/www/example.com/public_html/example-folder
The result should be similar to the following:
结果应类似于以下内容:
drwxrwxr-x 2 www-data exampleuser2 4096 Mar 29 16:34 example-folder
Therefore, the owner of example-folderhas successfully been changed to the PHP user: www-data.
因此,所有者example-folder已成功更改为 PHP 用户:www-data。
Done! PHP should now be able to write to the directory.
完毕!PHP 现在应该能够写入目录。
回答by sowrov
1st Figure out which user is owning httpd process using the following command
第一个使用以下命令找出哪个用户拥有 httpd 进程
ps aux | grep httpd
you will get a several line response like this:
你会得到这样的几行响应:
phpuser 17121 0.0 0.2 414060 7928 ? SN 03:49 0:00 /usr/sbin/httpd
Here 1st column shows the user name. So now you know the user who is trying to write files, which is in this case phpuserYou can now go ahead and set the permission for directory where your php script is trying to write something:
这里第一列显示用户名。所以现在您知道正在尝试写入文件的用户,在这种情况下phpuser您现在可以继续设置您的 php 脚本尝试写入内容的目录的权限:
sudo chown phpuser:phpuser PhpCanWriteHere
sudo chmod 755 PhpCanWriteHere
回答by Ryan Gyure
You can change the permissions of a folder with PHP's chmod(). More information on how to use the command is here: http://php.net/manual/en/function.chmod.php
您可以使用 PHP 的 chmod() 更改文件夹的权限。有关如何使用该命令的更多信息,请访问:http: //php.net/manual/en/function.chmod.php
If you get a 500 Error when setting the permissions to 777 (world writable), then it means your server is setup to prevent executing such files. This is done for security reasons. In that case, you will want to use 755 as the highest permissions on a file.
如果在将权限设置为 777(全局可写)时收到 500 错误,则意味着您的服务器已设置为防止执行此类文件。这样做是出于安全原因。在这种情况下,您将希望使用 755 作为文件的最高权限。
If there is an error_log file that is generated in the folder where you are executing the PHP document, you will want to view the last few entries. This will give you an idea where the script is failing.
如果在您执行 PHP 文档的文件夹中生成了一个 error_log 文件,您将需要查看最后几个条目。这将使您了解脚本失败的地方。
For help with PHP file manipulation, I use http://www.tizag.com/phpT/filewrite.phpas a resource.
为了帮助处理 PHP 文件,我使用http://www.tizag.com/phpT/filewrite.php作为资源。
回答by BSM
I found out that with HostGator you have to set files to CMOD 644 and Folders to 755. Since I did this based on their tech support it works with HostGator
我发现使用 HostGator 时,您必须将文件设置为 CMOD 644,将文件夹设置为 755。由于我根据他们的技术支持进行了此操作,因此它适用于 HostGator
回答by Jorge Gutierrez
I had the same problem:
我有同样的问题:
As I was reluctant to give 0777 to my php directory, I create a tmp directory with rights 0777, where I create the files I need to write to.
由于我不愿意将 0777 分配给我的 php 目录,所以我创建了一个权限为 0777 的 tmp 目录,在那里我创建了我需要写入的文件。
My php directory continue to be protected. If somebody hackes the tmp directory, the site continue to work as usual.
我的 php 目录继续受到保护。如果有人入侵了 tmp 目录,该站点将继续照常工作。
回答by nitishkrs
You can set selinux to permissive in order to analyze.
您可以将 selinux 设置为 permissive 以进行分析。
# setenforce 0
Selinux will log but permit acesses. So you can check the /var/log/audit/audit.logfor details. Maybe you will need change selinux context. Fot this, you will use chconcommand. If you need, show us your audit.logto more detailed answer.
Selinux 将记录但允许访问。所以你可以查看/var/log/audit/audit.log详情。也许您需要更改 selinux 上下文。为此,您将使用chcon命令。如果您需要,请向我们展示您audit.log更详细的答案。
Don't forget to enable selinux after you solved the problem. It better keep selinux enforced.
解决问题后不要忘记启用selinux。最好保持 selinux 强制执行。
# setenforce 1
回答by pritesh
Best way in giving write access to a directory..
授予对目录的写访问权限的最佳方法..
$dst = "path/to/directory";
mkdir($dst);
chown($dst, "ownername");
chgrp($dst, "groupname");
exec ("find ".$dst." -type d -exec chmod 0777 {} +");
回答by orjtor
I'm running Ubuntu, and as said above nobody:nobody does not work on Ubuntu. You get the error:
我正在运行 Ubuntu,如上所述没有人:没有人不能在 Ubuntu 上工作。你得到错误:
chown: invalid group: 'nobody:nobody'
Instead you should use the 'nogroup', like:
相反,您应该使用“nogroup”,例如:
chown nobody:nogroup <dirname>

