只有PHP的可读文件

I have another question:

when i write localhost/folder/file.txt into browser, it opens and show the content of file.txt.

But I want make this file readable only by PHP, not by browser.

I tried everything with chmod but it doesn't work. Is it possible to do that?

Thanks

You could refuse access to the .txt extension.

.htaccess

# prevent viewing of a specific file
<Files file.txt>
 order allow,deny
 deny from all
</Files>

# multiple file types
<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh|txt)$">
 Order Allow,Deny
 Deny from all
</FilesMatch>

You can put the text file into a mySQL database as BLOB or TEXT. So it becomes impossible to read by browser, only by query (through php).

Write to a file outside the web root, then the web server won't make it available to clients. (There is no requirement for a file to be under the document root for PHP to read it).

Other options include:

  • Using your webserver's auth/authz systems to secure the file (not recommended for this problem as it is more likely that a configuration error will break the security then it is that the file will be placed in the wrong place)
  • Using a database instead

Simplest solution :

$s=file_get_contents('test.txt');

If the file has some code to execute, you can eval it.

eval(file_get_contents('test.txt'));

Have you tried chmod it to 660 ?

I just tried it using my web server, it is not available.