Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using Linux 12.04,apache and php is installed on it.I want to access a text file in /root/ folder.I am really confused with the permissions.The php script i m using

<?php
$file = fopen("/root/cr.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached

while(!feof($file))
  {
  echo fgets($file). "<br>";
  }

fclose($file);
?> 

This script is able to access the file /var/www folder but not able to access /root/ip.txt file. Please help and explain step to step possible.

share|improve this question
1  
Double-check your code. You are saying /root/ip.txt in the comment but in the code you are writing /root/cr.txt –  karlingen Aug 6 '13 at 14:27
    
Edit your post and include the output of ls -l /root/cr.txt –  Braiam Aug 6 '13 at 14:58

2 Answers 2

up vote 1 down vote accepted

I will forget about the security implications of doing this and will go down to business:

If you have done ls -l /var/www and ls -l /root you would have noticed that both has different permissions:

$ ls -l /root/cr.txt
total 2
-rw-r-----  1 root root    0 Jul  9 01:28 cr.txt
$ ls -l /var/www
total 2
-rw-r--r--  1 www-data www-data    0 Jul  9 01:28 somefile

The /root is only readable for root, while /var/www is readable by www-data user. Now, if you check apache process you will notice that it's running using the www-data user.

$ ps aux | grep apache www-data 5133 0.0 0.2 6512 1208 ? R+ 10:04 0:00 apache

Now, you are trying to make apache running with the www-data user read the file. You can take three courses of action:

  1. Move the file to /var/www and change it's permissions so www-data users can read it.

    mv /root/cr.txt /var/www/
    chown www-data:www-data /var/www/cr.txt
    

    This is the preferable method.

  2. Create a symlink to the file in the /var/www directory:

    ln -s /root/cr.txt /var/www/
    

    This won't ensure that your file is being read, in some cases.

  3. This is dangerous and should not be done! Add the www-data user to the root group, or change the file ownership so it could be read by www-data users:

    chown :www-data /root/cr.txt
    ## Or
    sudo adduser www-data root
    

    This shouldn't be done if you don't understand the risks!

share|improve this answer
    
i have added the www-data to root ,but still it's not working.The script is not able to get content of /root/cr.txt –  user2656583 Aug 6 '13 at 14:57
    
Excellent and good warning. @user2656583 why not put the script in a cgi-bin -where- -it- -should- -be- and set apache up with a virtual host that allows executing from that path!? WHY create different methods when the normal ones that everyone else uses suffices? w h y? –  Rinzwind Aug 8 '13 at 18:28

Is the file readable by users other than root? Try adding read access for ALL.

sudo chmod a+r /root/cr.txt
share|improve this answer
    
yes the files is readable by everyone,but that's not solving the problem –  user2656583 Aug 6 '13 at 14:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.