Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 983
zeboidlund Avatar asked Sep 07 '12 03:09

zeboidlund


2 Answers

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

like image 52
Izack Avatar answered Oct 19 '22 20:10

Izack


Another reason of this error may be that the directory of file does not exist.

I just add php code before fopen:

if(!file_exists(dirname($file))) 
    mkdir(dirname($file));

This help me out.

like image 2
onebraveman Avatar answered Oct 19 '22 20:10

onebraveman