fopen():无法打开流:权限被拒绝(当路径权限为 777 时)

Roo*_*125 5 php linux permissions chmod

[重复]

fopen()当所有路径都具有 777 权限时,PHP 函数出现权限错误。
服务器详情:Centos 7 , PHP 7.1.8 , Apache 2.4.27

PHP源代码:

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
Run Code Online (Sandbox Code Playgroud)

错误信息 :

警告:fopen(newfile.txt):无法打开流:第 3 行 /var/www/html/test/file.php 中的权限被拒绝

我的测试网址: http://MyIPAddress/test/file.php

777 权限路径:

/var/www  
/var/www/html  
/var/www/html/test
/var/www/html/test/file.php
Run Code Online (Sandbox Code Playgroud)

具有权限的文件列表:( 编辑 1)

[root@localhost ~]# ls -la /var/www/html/test
total 8
drwxrwxrwx. 2 root root  41 Aug 24 20:26 .
drwxrwxrwx. 4 root root  48 Aug 24 19:37 ..
-rwxrwxrwx. 1 root root 179 Aug 24 20:22 file.php
-rwxrwxrwx. 1 root root   1 Aug 24 20:22 newfile.txt
Run Code Online (Sandbox Code Playgroud)

SELinux 已启用并具有访问列表:

[root@localhost ~]# semanage fcontext -l |grep "var/www"
/var/www(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0
/var/www/html(/.*)?                                all files          system_u:object_r:httpd_sys_rw_content_t:s0
/var/www/(/.*)?                                    all files          system_u:object_r:httpd_sys_content_t:s0
/var/www/html/(.*)?                                all files          system_u:object_r:httpd_sys_rw_content_t:s0
Run Code Online (Sandbox Code Playgroud)

(Edit2)*:如果我禁用 SELinux 问题将解决,问题来自 SELinux ,我将提出一个新问题,为什么当我有访问列表时也会出错。

小智 7

这听起来很奇怪,如果您拥有 root 权限并成功执行了以下操作:

sudo chmod 777 -R /var/www  
Run Code Online (Sandbox Code Playgroud)

comand fopen 应该可以正常运行,正如@Kimberlee 设置的那样,您应该尝试为新文件提供绝对路径,就像这样:

$file = fopen('/var/www/html/test/newfile.txt', 'w');
fwrite($file,'something');
chmod('/var/www/html/test/newfile.txt', 0777);
Run Code Online (Sandbox Code Playgroud)