【问题标题】:fopen(): failed to open stream: Permission denied (when path permission is 777) [duplicate]fopen():无法打开流:权限被拒绝(当路径权限为 777 时)[重复]
【发布时间】:2018-02-02 13:45:00
【问题描述】:

[重复]

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

PHP源码:

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

错误信息:

警告: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

具有权限的文件列表: (编辑 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

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

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

【问题讨论】:

  • 你能不能试着放一个绝对路径而不是相对路径来测试它是否适用于绝对路径。
  • @Kimberlee , Warning: fopen(/var/www/html/test/newfile.txt): failed to open stream: Permission denied
  • 你有在目录中创建的txt文件吗?如果没有,也许你可以尝试创建 txt 文件并再次尝试 fopen..
  • @Kimberlee 和第一个一样 :)
  • 如果文件已经创建,文件的权限是什么?

标签: php linux permissions chmod


【解决方案1】:

这听起来很奇怪,如果你有 root 权限并且已经成功完成了:

sudo chmod 777 -R /var/www  

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

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

【讨论】:

    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 2019-05-02
    • 2016-02-16
    • 1970-01-01
    • 2018-07-23
    • 2018-04-25
    • 2019-04-11
    • 2013-03-21
    相关资源
    最近更新 更多