file_get_contents () permission denied failed to open stream

1

I have, if I'm right, 4 hours (and followed) looking around the Stackoverflow forum (so they do not tell me to take a look at the tour), as well as multiple threads in other forums such as DigitalOcean, the solution to the famous problem of permission denied, failed to open stream, with the function file_get_contents()

I tried, by shell, doing multiple chmod , among these, 777 , as well as chown to the directory where the file I want to read is located, /root/test2/plugins/RLBT

This is my code: /var/www/html/bt_api.php

<?php
...
echo file_get_contents("/root/test2/plugins/RLBT/verified.json");

And the complete error:

Warning: file_get_contents(/root/test2/plugins/RLBT/verified.json): failed to open stream: Permission denied in /var/www/html/bt_api.php on line 34

Thanks for your answers.

    
asked by Carla Sanchez 22.04.2018 в 04:08
source

1 answer

1

The problem is that you try to read a file to which another user belongs; the /root folder belongs to user root while the user that is used to run apache or php is www-data .

You can check which user is running the script by writing this in a php file:

<?php echo exec('whoami'); ?>

normally the /var/www directory has permissions on the same user.

Now that you know there is a difference between users, you can take one of these three cases:

  

Move the file to the /var/www/html folder and change the permissions so that the user www-data can read it with:

$ sudo mv /root/test2/plugins/RLBT/verified.json /var/www/html/
$ sudo chown www-data:www-data /var/www/html/verified.json
  

Create a symbolic link (shortcut) to the file located in /root to the folder /var/www/html . In this method, if the file located in /root is modified by another user, the changes are automatically reflected in the link. For this:

$ sudo ln -s /root/test2/plugins/RLBT/verified.json /var/www/html/
$ sudo chmod 644 /root/test2/plugins/RLBT/verified.json
  

Add user www-data to group root , this method is dangerous since you will be giving permission to any user on the internet to access the server:

$ sudo adduser www-data root

Use at your own risk.

  

Here's the info: How to make php webpage to access the file in / root directory?

    
answered by 22.04.2018 в 05:09