Failed to open stream on file_put_contents in PHP on CentOS 7

13,090

Solution 1

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

Solution 2

chcon -Rv --type=httpd_sys_rw_content_t /dir

This may help,rather than turn off the selinux.

Share:
13,090
danielnovais92
Author by

danielnovais92

Updated on June 28, 2022

Comments

  • danielnovais92
    danielnovais92 almost 2 years

    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!