fopen() fails to open stream: permission d

2020-04-09 16:13发布

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?

2条回答
来,给爷笑一个
2楼-- · 2020-04-09 16:26

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

查看更多
Deceive 欺骗
3楼-- · 2020-04-09 16:39

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.

查看更多
登录 后发表回答