Making file in user's homedir accessible from web/webserver
The symbolic link will always have these permissions: lrwxrwxrwx. The "l" indicates that it is a link. Therefor, all users can follow the link because the effective permissions are those of the file to which the link points. It is never useful to try changing the permissions of a symbolic link.
The whole path from the root to the file in question must be readable by the Apache user. So... take a look the permissions of the home folder:
ls -la /
The home directory should be owned by root, group owned by root, with 0755 permission mask by default: drwxr-xr-x
. If not, change it:
sudo chown root:root /home; sudo chmod 0755 /home
Now take a look at your user's directory:
ls -la /home
If the username in question is sambo, the user's home directory should be owned by sambo, group owned by sambo, with 0755 permission mask by default: drwxr-xr-x
. If not, change it:
sudo chown sambo:sambo /home/sambo; sudo chmod 0755 /home/sambo
Repeat this as many times as needed, for however many directories exist until you reach the directory that contains the file. Then, don't forget about the file itself.
sudo chown sambo:sambo /home/sambo/path/to/file/file.txt; sudo chmod 0644 /home/sambo/path/to/file/file.txt
Then use PHP to open it:
<?php
$file = '/home/sambo/path/to/file/file.txt';
$fp = fopen($file, 'r');
You could use the symbolic link at this point, too.
Related videos on Youtube
Comments
-
Sourabh over 1 year
I have a txt-file one of my user's homedir which is regularly updated there by a script. I now want to be able to access (read) this file from the web.
/home/user/folder/file.txt
So what I tried now is to log in as root, go into my webservers httpdocs folder
/var/www/path/to/domain/httpdocs
and there I tried to create a symbolic link with
ln -s /home/user/foler/file.txt /var/www/path/to/domain/httpdocs/file.txt
But this didn't work... I already tried changing the chmod of the symlink (which changes the ones from the original file of course) and also a chown to the user from webserver, but no matter what I tried I cannot open the file from the web or from a php-script (which is what I want to do)
Can anybody help me and tell me what I need to do? What rights do I need to give? Or is there another way of achieving this?
-
jet about 13 yearscan you try
chmod o+r /home/user/folder/file.txt
-
Lekensteyn about 13 yearsPossibly duplicate of Permissions issue: how can Apache access files in my Home directory?.
-
-
Sourabh about 13 yearsWell I've tried that... I set the whole path from home to the directory of the file to chmod 0755 and the file itself too. But still the script cannot read the file nor can I directly open it yet...
-
Javier J. Salmeron Garcia about 13 yearsAre you running PHP in safe_mode? (You would have to to chown your file to www-data. 'chown www-data file.txt'). Any errors in Apache log file?
-
Sourabh about 13 yearsI went the other way now which worked for me... I had teh script write it directly into the /var/www/... path, but I needed your chmod changes here to have it writable there... thank you!