PHP fopen always fails with permission denied

Solution 1:

Good question -- shows you've done your homework.

I can't think of anything "obvious" that you've failed to check, so I can only give you some more advanced debugging advice.

  • Fire up strace on the PHP process, and see exactly what it's doing under the hood.
  • Write a bit of code that does an fopen(..., 'r') to triple-check that your directory tree permissions are, in fact, OK.
  • su to the user in question and try doing some stuff on the command line. If that works, then you know your script isn't running with the permissions you think it is.
  • Check that there are no extended ACLs on the file, directory, or leading path components (getfacl is the tool to use). It's uncommon, but every once in a while they'll leap up and rip your face off. If you do have ACLs, use setfacl -x to get rid of them once you've verified they're unnecessary.

Solution 2:

Check what is the home folder of apache (normally it is /var/www) and put the files under this folder, finally then give necessary permissions. Change the php temp upload folder to this. This is working for me.

Solution 3:

try /usr/sbin/apachectl -t -D DUMP_MODULES

check that all modules enabled

If you get

Warning: SuexecUserGroup directive requires SUEXEC wrapper.

Resolution

Such warning is usually caused by invalid permissions on suexec wrapper, it should be:

# ls -la /usr/sbin/suexec
-rwsr-xr-x 1 root root 12064 2008-04-17 01:15 /usr/sbin/suexec

If the permissions or ownership are differ from the example above, use the following commands to correct them:

# chown root:root  /usr/sbin/suexec
# chmod 4755 /usr/sbin/suexec

after this fix everything was ok.