Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 允许脚本读取文件,但禁止用户直接查看文件_Php_Linux - Fatal编程技术网

Php 允许脚本读取文件,但禁止用户直接查看文件

Php 允许脚本读取文件,但禁止用户直接查看文件,php,linux,Php,Linux,假设我有一个纯文本文件example.txt,我的web服务器上有一个PHP脚本readfile.PHP 我希望能够阻止用户键入http://www.example.com/example.txt并直接查看文本文件,但我仍然希望人们能够加载http://www.example.com/readfile.php它读取文件example.txt并对其进行处理(可能显示文件的内容) 此外,如果可以做到这一点,最好的方法是什么?只需将您不希望公开访问的文件存储在webroot之外即可 /home

假设我有一个纯文本文件
example.txt
,我的web服务器上有一个PHP脚本
readfile.PHP

我希望能够阻止用户键入
http://www.example.com/example.txt
并直接查看文本文件,但我仍然希望人们能够加载
http://www.example.com/readfile.php
它读取文件
example.txt
并对其进行处理(可能显示文件的内容)


此外,如果可以做到这一点,最好的方法是什么?

只需将您不希望公开访问的文件存储在webroot之外即可

/home
   example.txt
   /www
      readfile.php

如果
/home/www/
是您的公共webroot文件夹,则无法通过web服务器访问它上面的任何文件。
readfile.php
仍然可以在
。/example.txt
上访问该文件。

您可以将该文件放入“example.txt”在公用文件夹之外,从readfile.php读取,如
$content=file\u get\u contents(“../example.txt”)如果需要将文件存储在webroot中,则将文件放在一个文件夹中并拒绝访问该文件夹。如果使用apache,则在文件夹中创建一个.htaccess文件,然后键入
deny from all

您可以这样调用一个文件,但用户看不到该文件名:

<?php
$file = 'example.txt';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

(来源:php.net)


这对你有用吗?

是的,这很容易做到

阻止用户访问
example.txt
有两种主要方法。第一种方法是将其放入web文件夹外的文件夹(通常称为
www
public\u html
),第二个是将一个
.htaccess
文件与example.txt脚本一起放入文件夹中,该脚本将完全阻止对该文件的访问

<files "example.txt"> 
deny from all 
</files>

全盘否定
但是,如果要阻止文件夹中的所有
.txt
文件,可以将
example.txt
更改为类似
*.txt
的内容


然后,您可以在
readfile.php
中使用来获取文本文件的内容,或者如果您只想输出文件,您可以使用我也做过类似的操作,其中文件包含极其敏感的信息,我只希望经过验证的用户能够通过HTTPS连接检索文件

我所做的是:

我将文件放在一个目录路径中,该路径超出了web服务器的范围(Apache,对我来说)可以看到。因此,没有可能的URL会导致文件直接由web服务器提供。然后,我创建了一个脚本,允许用户登录,单击他们想要的文件,然后PHP脚本读取文件,放入适当的标题,然后将文件流式传输到用户的计算机

当然,向用户显示文件列表的脚本和将文件流式输出给用户的脚本必须至少对存储路径中的文件具有读取权限


祝你好运!!

快速破解-将你的文件重命名为“.cannotReadFileWithDOT”。服务器将关闭读取名称开头带点的文件,但您的脚本将能够读取这些文件。另外,apache和nginx现成服务器配置为禁止读取名称开头带点的文件。

您的意思是“…希望人们能够加载”而不是“readfile.txt”.WOW…为什么我没有想到这一点?谢谢!+1将文件放入web目录并使用.htaccess。谢谢。我添加了.htaccess文件,但是在尝试调用file_get_contents()时出错::无法打开流:HTTP请求失败!HTTP/1.1 403被禁止。我必须更改php访问文件的任何权限吗?我还尝试使用cUrl,但没有成功。在.htaccess中,使用:SecFilterEngine Off deny from all