我有 3 个子文件夹upload
文件夹。我的代码如下所示:
if(isset($_SESSION["u_type"]) && $_SESSION["u_type"] == 3) {
$files = scandir($path."/3/") //$path is set somewhere above
//...
}
它工作正常,但你实际上可以添加一个简单的 html 标签,例如<img src="uploads/2/somathing.png/>
即使您的“用户类型”设置为 3,您也可以从子目录 2 获取任何文件。
有办法预防吗?
我已经尝试过:
我正在使用 .htaccessOptions- Indexes
在其中,但它只是不直接列出文件。
是的,有一种方法可以做到这一点,它需要 Apache Web 服务器(对于 .htaccess 文件):
拒绝上传文件夹中的所有内容
将其添加到您的上传文件夹中.htaccess
:
order deny,allow
deny from all
创建代理 php 脚本image.php
创建一个名为image.php
并在那里检查您的会话:
(您可能需要根据您的要求更新此脚本,这是一个仅支持 jpeg 的简单示例)。
<?php
session_start();
//check session for permission here!
$userTypeId = 1; //change this to requirements
header('Content-type: image/jpeg');
echo file_get_contents("uploads/" . $userTypeId . "/" . $_GET['image']);
现在通过访问代理脚本来访问图像
<img src="image.php?image=yourimage.jpg" />
更新;你可以选择重写image.php?image=x.jpg
您可以选择重写此路径.htaccess
所以看起来好像没有代理.php
工作中的脚本。
RewriteRule ^/a_chosen_path_name/([^\.]+)\.(png|jpg|gif)$ /image.php?image=$1.$2 [NC,L]
之后您可以使用:
<img src="a_chosen_path_name/yourimage.jpg" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)