I am confused with this code:
test.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:
-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.
try
fopen('/path/to/file/test.txt','a+');
as
fopen('test.txt','a+');
is most likely looking in another directory
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 subdirectories on the server to "apache", I did a "chmod 777" on all the files, directories, subdiretories, I ran "restorecon -R", I ran "chcon unconfined_u:object_r:httpd_user_content_t:s0" on all the files, but the only thing that seemed to work is turning SELinux off completely.
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.
$ getsebool -a | grep httpd
This gave a list of about 36 parameters.
I painfully went and turned on every boolean using the setsebool command until the "failed to open stream. permission denied" error went away.
When I turned "on" the httpd_unified boolean, the error went away!!
When I turned it "off", the error came back!!
This issue can also be a result of having SELinux enabled. This can be solved using:
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
Use fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','a+');
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.
Check the SELinux context mode of the file to be sure it is set to: httpd_user_content_t
...If it's not, use:
chcon httpd_user_content_t test.txt
...to correct the problem.