Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to make a file on my server be readable by PHP, but not directly accessible with a browser. Say my Apache installation's root is /apache. I'd like PHP to be able to get the text of /apache/1.txt, but not allow the user to go to domain.tld/1.txt and view the contents. I'd still like to be able to read/write the file through FTP/SFTP without root.

What would be the propper chmod string for this? 642 seems likely, but I'm not sure how PHP accesses the file.

share|improve this question
What are you trying to accomplish? (Also, making the file world writable is not going to help, so don't do that.) – Michael Hampton Jan 12 '13 at 16:12
@MichaelHampton I'm trying to have a bunch of files with data to load into a page, and I'm too lazy to make a database, since it will bever really be edited. – tkbx Jan 12 '13 at 16:20
The real correct answer is to not put files you don't want the webserver to serve to people below DocumentRoot. Make a directory somewhere else and have the files loaded from there. – DerfK Jan 12 '13 at 17:43
up vote 3 down vote accepted

Redirect all request coming to /1.txt to something else using .htaccess or apache configuration. So, if some one will try to access domain.tld/1.txt, it will redirect.



suku@ubuntu-vm:/var/www/html$ grep info.php /etc/apache2/sites-available/default
        RedirectMatch ^/info.php /html/index.html

suku@ubuntu-vm:/var/www/html$ sudo cat ../info.php 

suku@ubuntu-vm:/var/www/html$ cat index.html 
You redirected to here becuase you tried to access localhost/info.php
suku@ubuntu-vm:/var/www/html$ pwd

suku@ubuntu-vm:/var/www/html$ links http://localhost/info.php -dump
You redirected to here becuase you tried to access localhost/info.php

suku@ubuntu-vm:/var/www/html$ cd ..
suku@ubuntu-vm:/var/www$ ls -l info.php 
-rw-r----- 1 www-data www-data 20 Jan 12 11:25 info.php
share|improve this answer
And then just 755? – tkbx Jan 12 '13 at 16:18
640 is more than enough – Suku Jan 12 '13 at 16:21
@tkbx See my edit – Suku Jan 12 '13 at 16:34

Your Answer


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.