0

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.

tkbx
  • 201
  • 1
  • 2
  • 6
  • 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

1 Answers1

3

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.

EDIT

Example:

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 
<?php
phpinfo();
?>

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
/var/www/html

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
Suku
  • 2,036
  • 13
  • 15