1

I have created a folder /home/UploadContent with owner:group as apache:apache, the same group and user as the php process. I then used chmod to set the permissions to 775, which should allow php to write uploaded files to this directory. I tried it, and it gave me the same insufficient permissions error.

The exact error is "failed to open stream: Permission denied".

I wrote a small php program to test whether or not: $Owner = fileowner("/home/UploadContent/"); $Current = exec("whoami"); $Id = exec("id -u " . $Current); echo ($Owner . " is owner, " . $Id . " is user.");

This always returns "48 is owner, 48 is user."

This would imply that php has the correct permissions to write,read, and execute, but it is still throwing errors.

  • 1
    Sounds like an SELinux problem... Try running `setenforce 0` at a bash prompt and then re-run the script. If this resolves the issue, you need to configure SELInux to allow your process access to the specified folder. You can re-enable SELInux with `setenforce 1`. Failing that, SELinux will be re-enabled at boot. – Basic Sep 24 '16 at 17:12
  • also try wifh perm 777. then. check what are the owner/group etc – Nick Sep 24 '16 at 17:14
  • Ok @Basic, that worked when I ran setenforce 0. I will configure SELInux. – DeadMansMarch Sep 24 '16 at 17:17
  • Glad I could help. On Centos/RedHat, I use [audit2allow](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Fixing_Problems-Allowing_Access_audit2allow.html) to make appropriate policies by analysing the audit log. I have no idea what the standard is on other distros. This link may also be of use: https://wiki.centos.org/HowTos/SELinux – Basic Sep 24 '16 at 17:19
  • 1
    @Basic is there any other way to setup SELInux? Like maybe a pure config file? Also, could you format your comment as an answer because I am sure other people would be helped by this answer. – DeadMansMarch Sep 24 '16 at 17:21
  • chcon command worked fully for me – DeadMansMarch Sep 24 '16 at 17:31
  • Done, I included chcon too. Glad we got there in the end. – Basic Sep 24 '16 at 17:34
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 24 '16 at 19:35

1 Answers1

0

[Promoted from a comment]

Sounds like an SELinux problem...

Try running setenforce 0 at a bash prompt and then re-run the script. If this resolves the issue, you need to configure SELinux to allow your process access to the specified folder.

You can re-enable SELinux with setenforce 1. Failing that, SELinux will be re-enabled at boot.

On Centos/RedHat, I use audit2allow to make appropriate policies by analysing the audit log. I have no idea what the standard is on other distros. This Wiki page explains what SELinux does and why it's important if you want your server to be secure.

I'm not aware of any way to configure SELinux via config files, however you can use chcon to change the security context of a specific resource (like a file). More details of how security contexts work can be found here.

Finally, you can disable SELinux entirely but this is not recommended.

As pointed out by @VicSeedoubleyew in comments, there's a helpful checklist for resolving this and similar issues available here.

Community
  • 1
  • 1
Basic
  • 26,321
  • 24
  • 115
  • 201
  • There is a troubleshooting checklist for this error, which also explains how to deal with SELinux : http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Sep 24 '16 at 19:36
  • 1
    @VicSeedoubleyew Useful, thanks. I'll include a direct link – Basic Sep 24 '16 at 22:14