fopen() fails to open stream: permission denied, yet permissions should be valid

zeboidlund picture zeboidlund · Sep 7, 2012 · Viewed 49.6k times · Source

So, I have this error:

Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied

Performing ls -l in the directory where test-in.txt is produces the following output:

-rw-r--r-- 1 $USER $USER 1921 Sep  6 20:09 test-in.txt
-rw-r--r-- 1 $USER $USER    0 Sep  6 20:08 test-out.txt

In order to get past this, I decided to perform the following:

chgrp -R www-data /path/to/php/webroot

And then did:

chmod g+rw /path/to/php/webroot

Yet, I still get this error when I run my php5 script to open the file. Why is this happening? I've tried this using LAMP as well as cherokee through CGI, so it can't be this.

Is there a solution of some sort?

Edit

I'll also add that I'm just developing via localhost right now.

Update - PHP fopen() line

$fullpath = $this->fileRoot . $this->fileInData['fileName'];

$file_ptr = fopen( $fullpath, 'r+' );

I should also mention I'd like to stick with Cherokee if possible. What's this deal about setting file permissions for Apache/Cherokee?

Answer

Izack picture Izack · Sep 7, 2012

Check if the user that PHP runs under have "X" permission on every directory of the file path.
It will need it to access the file

If your file is: /path/to/test-in.txt
You should have X permission on:

  • /path
  • /path/to

and read permission on /path/to/test-in.txt