PHP can't read file with read permissions

2019-07-27 10:08发布

问题:

I have a file with these permissions (0644)

-rw-r--r-- 1 root root

So from my understanding this file should be readable by anyone.

Now I have this code:

file_get_contents('/path/to/file');

And it is giving me this

Warning: file_get_contents(...): failed to open stream: Permission denied in ...

What am I missing?

I'm running it from a web server under www-data.

回答1:

Possible causes with solutions:

Reading permissions are required for folders in the file path

Solution: Configure the correct permission rights for the whole folder hierarchy. Also see this remark on PHP safe mode.

Execution permissions are required for reading the file

It's strange, but this happens.

Solution: Configure execution rights for file and folders.

Access control lists for the server are not configured

Solution: Configure them properly.



回答2:

Login with root user and execute the command:

chmod -R 777 /path/to/file

OR

chmod -R 777 /path