为什么file_put_contents在从浏览器运行时有权限问题?
This question has been asked a couple of times up here, but I haven't found a solution yet. I have a Fedora 19 LAMP server and I just want to run the simple command: file_put_contents('test.txt', 'Hello there');
in order to confirm that my web server can use PHP to write data to files. I'm having trouble figuring out a proper permissions scheme. To start, just for development, Apache's document root is /var/www/html
. This directory was originally owned by a user and group called www-data
, but I changed the directory's group to the primary group of the owner of the httpd process, named apache
. It is this owner that is active when PHP runs. I've confirmed this with the following:
这个问题在这里被问过几次,但我还没有找到解决方案。我有一个Fedora 19 LAMP服务器,我只想运行简单的命令:file_put_contents('test.txt','Hello there');为了确认我的Web服务器可以使用PHP将数据写入文件。我无法找到合适的权限方案。首先,只是为了开发,Apache的文档根目录是/ var / www / html。该目录最初由名为www-data的用户和组拥有,但我将目录的组更改为httpd进程的所有者的主要组,名为apache。这个所有者在PHP运行时处于活动状态。我已通过以下方式证实了这一点:
As you see, the process owner is apache
, the current direcory is /var/www/html/php-console
. The directory is owned by www-data
and members of the group apache
have full access to it.
如您所见,进程所有者是apache,当前的目录是/ var / www / html / php-console。该目录由www-data拥有,组apache的成员可以完全访问它。
I have tried the following to get PHP to actually create a file in this location, but to no avail:
我已尝试以下方法让PHP在这个位置实际创建一个文件,但无济于事:
chmod 777 /var/www/html/php-console
chown apache /var/www/html/php-console
chgrp apache /var/www/html/php-console
cd /var/www/html; > test.txt; chmod 777 test.txt;
Nothing will work while this script is run from the browser. However, when I use file_put_contents
with the PHP CLI, it works just like I would expect, provided that the user I'm entering commands as or its group has write permissions to this directory or test file.
从浏览器运行此脚本时,什么都不起作用。但是,当我使用带有PHP CLI的file_put_contents时,它就像我期望的那样工作,只要我输入命令的用户或其组具有对此目录或测试文件的写权限。
So, from the command line, you see how www-data
has read, write, and execute permissions to the folder I'm in. posix_getpwuid
and posix_geteuid
help you to find the owner of the Apache/PHP process, which in this case is the same as the user logged into the console. file_put_contents
succesfully writes 8 bytes to the specified file. If I change the group or owner and group to something else, I get Permission denied, which absolutely makes sense.
因此,从命令行,您可以看到www-data如何读取,写入和执行我所在文件夹的权限.posix_getpwuid和posix_geteuid可帮助您找到Apache / PHP进程的所有者,在本例中为与登录控制台的用户相同。 file_put_contents成功地将8个字节写入指定的文件。如果我将组或所有者和组更改为其他内容,我会拒绝Permission,这绝对有意义。
If this works on the command line, then why not when I really want it to, i.e., while actually serving web pages???
如果这在命令行上有效,那么为什么不在我真正想要的时候,即在实际提供网页时呢?
1 个解决方案
#1
1
Because you forgot to read the httpd_selinux(8)
man page and give the directory the appropriate file context to allow the web server to write files there.
因为您忘记阅读httpd_selinux(8)手册页并为目录提供适当的文件上下文以允许Web服务器在那里写入文件。
更多相关文章
- php exec文件从终端运行,而不是从浏览器运行
- log4php将不同级别的日志打印到不同的日志文件中
- PHP - 在外部服务器上读取文件?
- 仅在LARAVEL或AJAX中提交表单时,无需用户身份验证即可将文件上载
- PHP ZipArchive 实现压缩解压Zip文件
- 纯php文件不加结束标记的好处
- 大文件从指定行开始读取
- 找不到go-pear。bat文件PHP 5.5 Windows 7。
- 将csv文件保存到现有文件,但在新工作表和重命名工作表中