Failed to open stream on file_put_contents in PHP on CentOS 7

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 05:49:07

问题


I know this is a common bug with many threads but I'm tired of searching and I still haven't solved it. When I try to run the function file_put_contents I get the following error:

Warning: file_put_contents(test.txt): failed to open stream: Permission denied in /var/www/html/nurses/adminWriter.php on line 29

I've already done the most common answer, change the permissions of the nurses folder and the test.txt file to 777. That didn't work.

I'm currently on a fresh install of the latest CentOS 7 server.

I've found that if I run the php script through the console:

# php /var/www/html/nurses/adminWriter.php

It works just fine, I get the error only when running it through the browser, but I really need to run it this way.

Best Regards!


回答1:


chcon -Rv --type=httpd_sys_rw_content_t /dir

This may help,rather than turn off the selinux.




回答2:


Ok, I found the issue: selinux

Here's the solution: http://kb.sp.parallels.com/en/11142

In case the page goes down:

Symptoms

After you change the default directory to store website content (say, to "/home"), you are unable to access the content of subdomains and additional websites over the Web.

Cause

This problem occurs if SELinux works in "Enforcement" mode. In this mode, webroot directories for websites and subdomains inside them have different permissions, and your web server fails to access them. Run this command:

# getenforce

The output should be: Enforcing

Resolution

Disable SELinux support: Open the file /etc/selinux/config in any editor and find this row:

SELINUX=<value>

Change it to:

SELINUX=disabled

If you cannot find this file, open the file /boot/grub/grub.conf in any editor and add the parameter selinux=0 to the Grub Boot Loader:

title SE-Linux Test System
root (hd0,0)
kernel /boot/vmlinuz-2.4.20-selinux-XXXXXXXXXX ro root=/dev/hda1 nousb selinux=0
#initrd /boot/initrd-2.4.20-selinux-XXXXXXXXXX.img

Reboot the server to apply the changes. If you do not want to reboot, run this command:

# setenforce 0


来源:https://stackoverflow.com/questions/28786047/failed-to-open-stream-on-file-put-contents-in-php-on-centos-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!