5

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!

6
  • 1
    When you run a php script through apache, it runs as (whatever user you specify), often "apache". When you run a php script through the console, you're running it as whatever you're logged in as. Ensure the "apache" (or whatever) user can access that file (which includes any parent directories). Also, you may try using an absolute path to test.txt -- you may be trying to write somewhere you're not expecting.
    – jedwards
    Feb 28, 2015 at 19:46
  • Did you skip the quote by purpose ? Try that : file_put_contents(__DIR__ . '/test.txt');
    – alfallouji
    Feb 28, 2015 at 19:52
  • You mean doing something like: # chown -R apache:apache /var/www/html/nurses/ Feb 28, 2015 at 19:52
  • Yes I'm doing: file_put_contents('teste.txt', 'this is my content'); Already tried with full path to file a with ( '/var/www/html/nurses' . 'teste.txt' ) Feb 28, 2015 at 19:55
  • The code from your comment is missing a forward slash: '/var/www/html/nurses/'.'teste.txt'
    – jedwards
    Feb 28, 2015 at 20:04

2 Answers 2

20
chcon -Rv --type=httpd_sys_rw_content_t /dir

This may help,rather than turn off the selinux.

3
  • 3
    Could you explain what this does please?
    – Mike
    Jul 4, 2017 at 9:00
  • thanks aureport -a before run chcon {{{ 24. 09-10-2017 16:25:31 httpd system_u:system_r:httpd_t:s0 2 file read unconfined_u:object_r:user_home_t:s0 denied 923 25. 09-10-2017 16:26:42 httpd system_u:system_r:httpd_t:s0 2 file read unconfined_u:object_r:user_home_t:s0 denied 924 26. 09-10-2017 16:29:38 httpd system_u:system_r:httpd_t:s0 2 file read unconfined_u:object_r:user_home_t:s0 denied 925 }}}
    – Sérgio
    Oct 9, 2017 at 15:35
  • This one worked for me. U saved my time after working for many hours. use sudo chcon -Rv --type=httpd_sys_rw_content_t /dir
    – Vara
    Dec 26, 2017 at 11:41
7

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
5
  • 1
    Disabling SELinux is not recommend. There are better ways of doing it.
    – Ilia
    Sep 5, 2015 at 12:34
  • 2
    @Ilia Rostovtsev I ran into this same problem, and disabling SELinux fixed it, what other better ways of doing it without disabling SELinux ?
    – technomage
    Oct 27, 2015 at 14:21
  • @IliaRostovtsev What are the "better ways of doing it" ??
    – emmdee
    Apr 10, 2019 at 17:36
  • It was years ago, and I can not remember exactly now, what I did to solve this right but I'm sure, that I applied the rules and/or changed context. Familiar to what is described here: stackoverflow.com/a/27501934/1455661
    – Ilia
    Apr 10, 2019 at 19:01
  • strange i don't know what is SELINUX & why it is not recommended to disable it but beside that this solution works like a charm in my case. Nov 4, 2020 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.