PHP:fopen() 权限被拒绝

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

PHP: fopen() Permission denied

phppermissions

提问by T_t

I am confused with this code:

我对这段代码感到困惑:

test.php:

测试.php:

fopen('test.txt','a+');

when I execute it, I get an error:

当我执行它时,我收到一个错误:

Warning: fopen(test.txt): failed to open stream:
     Permission denied in /var/www/html/yuelu3/mobile/text.php on line 2

test.txt:

测试.txt:

-rwxrwxrwx. 1 jt jt     87 10月  7 20:58 test.txt

where is the problem?

问题出在哪儿?

Thanks a lot!I have found the problem,I use FC13,because of the protect of SELinux,some action is denied.So, I just need to get rid of the protect.

非常感谢!找到问题了,我用的是FC13,因为SELinux的保护,一些动作被拒绝了。所以,我只需要摆脱保护。

回答by mistapink

This issue can also be a result of having SELinux enabled. This can be solved using:

此问题也可能是启用 SELinux 的结果。这可以使用以下方法解决:

chown -R apache:apache /var/www/html/directory_to_write
chcon -R -t httpd_sys_content_t /var/www/html/directory_to_write
chcon -R -t httpd_sys_rw_content_t /var/www/html/directory_to_write

You can find more about the contexts at https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Managing_Confined_Services/sect-Managing_Confined_Services-The_Apache_HTTP_Server-Types.html

您可以在https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Managing_Confined_Services/sect-Managing_Confined_Services-The_Apache_HTTP_Server-Types.html找到有关上下文的更多信息

回答by Phill Pafford

try

尝试

fopen('/path/to/file/test.txt','a+');

as

作为

fopen('test.txt','a+');

is most likely looking in another directory

很可能在另一个目录中查找

回答by kwatts

I have been receiving the same error "failed to open stream. permission denied"when trying to write a file on the server using PHP. I tried everything on the internet to fix the error. I changed ownership of the files,directories and sub-directories on the server to "apache", I did a chmod 777on all the files, directories, sub-directories, I ran restorecon -R, I ran chcon unconfined_u:object_r:httpd_user_content_t:s0on all the files, but the only thing that seemed to work is turning SELinux off completely.

尝试使用 PHP 在服务器上写入文件时,我一直收到相同的错误“无法打开流。权限被拒绝”。我尝试了互联网上的所有方法来修复错误。我将服务器上的文件、目录和子目录的所有权更改为“apache”,我chmod 777对所有文件、目录、子目录进行了操作restorecon -R,我运行chcon unconfined_u:object_r:httpd_user_content_t:s0了所有文件,但唯一似乎是工作正在完全关闭 SELinux。

I finally resolved the issue. The problem lay in the boolean parameters used by SELinux. I performed the following command to get a list of all the booleans related to httpd.

我终于解决了这个问题。问题在于 SELinux 使用的布尔参数。我执行了以下命令来获取与 httpd 相关的所有布尔值的列表。

$ getsebool -a | grep httpd

This gave a list of about 36 parameters.

这给出了大约 36 个参数的列表。

I painfully went and turned on every boolean using the setsebool command until the "failed to open stream. permission denied" error went away.

我痛苦地使用 setebool 命令打开每个布尔值,直到“无法打开流。权限被拒绝”错误消失。

When I turned "on" the httpd_unified boolean, the error went away!! When I turned it "off", the error came back!!

当我“打开”httpd_unified 布尔值时,错误消失了!!当我将其“关闭”时,错误又回来了!!

回答by Warrior

Use fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','a+');

fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','a+');

回答by Kyle

Paths always cause issues when trying to open files. An easy way to avoid having any problems when you are attempting to open files is by checking what directory you are in, and where you are making the call to.

尝试打开文件时,路径总是会导致问题。避免在尝试打开文件时出现任何问题的一种简单方法是检查您所在的目录以及调用的位置。

echo getcwd();

Create a simple PHP page and make a call print the current directory. Drop the PHP page into the same directory as the file, and it will tell you the correct path to the folder and then just add on the /filename.xxx.

创建一个简单的 PHP 页面并调用打印当前目录。将 PHP 页面放到与文件相同的目录中,它会告诉您文件夹的正确路径,然后只需添加 /filename.xxx。

回答by vdbuilder

Check the SELinux context mode of the file to be sure it is set to: httpd_user_content_t

检查文件的 SELinux 上下文模式以确保其设置为:httpd_user_content_t

...If it's not, use:

...如果不是,请使用:

chcon httpd_user_content_t test.txt

...to correct the problem.

...纠正问题。

回答by vdbuilder

I'm using an Apache HTTP server v2.4, I think the following method will work for most people on linux:
first thing check who is the user running 'httpd'.
It is daemonin my case, and I'm not sure if it is the case for everybody. you can do so using:
ps -ef | grep 'httpd'

我使用的是 Apache HTTP 服务器 v2.4,我认为以下方法适用于 Linux 上的大多数人:
首先检查谁是运行“httpd”的用户。
这是daemon我的情况,我不确定是否对每个人都是如此。你可以这样做:
ps -ef | grep 'httpd'

once you're sure that the user daemonis the owner of the process httpd, change the owner of the folder in which you want the permission to write to deamon

一旦您确定用户daemon是进程httpd的所有者,请更改您想要写入权限的文件夹的所有者deamon

sudo chown daemon /path/to/folder

sudo chown daemon /path/to/folder

this will change only the user, the group of that directory remain unchanged. if you want the group also to be able to write, make sure that it has write permission

这只会更改用户,该目录的组保持不变。如果您希望该组也能够写入,请确保它具有写入权限

sudo chmod g+w /path/to/folder

sudo chmod g+w /path/to/folder