Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen(): failed to open stream: Permission denied (when path permission is 777) [duplicate]

[Duplicated]

PHP function fopen() got permission error when all the path is with 777 permission.
Server details : Centos 7 , PHP 7.1.8 , Apache 2.4.27

PHP Source Code :

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

Error Message :

Warning: fopen(newfile.txt): failed to open stream: Permission denied in /var/www/html/test/file.php on line 3

MyTest Url : http://MyIPAddress/test/file.php

777 permission paths :

/var/www  
/var/www/html  
/var/www/html/test
/var/www/html/test/file.php

File list with permissions : (Edit 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 is enabled & have access-list :

[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)* : If I disable SELinux the problem will solve , problem is from the SELinux , I'll make a new Question about it that why got error when I have access-list too.

like image 930
Root Avatar asked Aug 24 '17 15:08

Root


1 Answers

That sounds strange, if you have root permissions and have successfully done a:

sudo chmod 777 -R /var/www  

The comand fopen should run without problems, as @Kimberlee set, you shuld try to provide an absolute path for the new file, just as this:

$file = fopen('/var/www/html/test/newfile.txt', 'w');
fwrite($file,'something');
chmod('/var/www/html/test/newfile.txt', 0777);
like image 110
Samuel PS Avatar answered Sep 25 '22 00:09

Samuel PS