【发布时间】:2016-06-28 02:16:51
【问题描述】:
基于这两个问题:
我写了一个简单的密码保护文件上传器。
所以我有一个名为 password.php
的密码验证器 <body>
<?php
$pass = $_POST['pass'];
{?>
<form method="POST" action="password.php">
Password <input type="password" name="pass"></input>
<input type="submit" name="submit" value="Ok"></input>
</form>
<?}
if($pass == "admin")
{
include("../uploader.html");
}
?>
</body>
从代码中可以看出。如果通过正常,它包括 public_html 文件夹中的 uploader.html。
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input name="thefile" type="file" /><br>
<input type="submit" value="Send" />
</form>
</body>
最后我们使用upload.php将“thefile”发送到名为files的文件夹中。
<body>
<?php
$thefile_tmp = $_FILES['thefile']['tmp_name'];
$thefile_name = $_FILES['thefile']['name'];
if(is_uploaded_file($thefile_tmp))
{
move_uploaded_file($thefile_tmp, "files/$thefile_name");
}
?>
</body>
我将 files 文件夹的 chmod 更改为 777,一切正常。
我把密码给了一群学生,我让他们给我发报告。学生现在可以匿名上传恶意文件(比如说 loop.php)并在浏览器中打开它.../files/loop.php。
所以我想保护我的网站免受此类报道的影响。我尝试将 files 文件夹的 chmod 设置为 773 或 776。在 776 上,我的代码无法发送文件。在 773 上,您仍然可以打开 files 文件夹中的内容。
问题/请求有没有一种简单的方法可以阻止打开上传的文件?
我使用的好简单的解决方案
根据@Fred -ii- 的建议,我在 upload.php 中添加了一个扩展验证程序。现在是这个样子
<body>
<?php
$thefile_tmp = $_FILES['thefile']['tmp_name'];
$thefile_name = $_FILES['thefile']['name'];
$thefile_ext = pathinfo($thefile_name, PATHINFO_EXTENSION);
if(is_uploaded_file($thefile_tmp))
{
if( $thefile_ext == 'zip')
{
move_uploaded_file($thefile_tmp, "files/$thefile_name");
}
}
?>
</body>
替代解决方案
正如@alexander.polomodov 的回答。我只是将 .htaccess 文件放在 files 文件夹中,仍然可以发送文件,但没有人可以访问它。这种方法不适合我,因为我希望学生能够偷看彼此的报告。
【问题讨论】:
-
您可以在上传时从文件中删除 .php 扩展名。因此,当服务器收到对最近上传文件的请求时,它不会执行该文件。
-
“我要求提供 .zip 文件” - 是的,在 cmets 中。不在你的问题中。是什么阻止您从可能的欺骗中添加到阵列中?你没有看完里面的所有答案吗?
-
您删除了关于“我要求提供 .zip 文件” 的评论,现在您不再回复。看看下面那个人的回答,那么其中一个链接可能是骗子之一。
-
@Fred-ii- 对不起。我没有赶上。现在我正在尝试检查我的服务器上是否有 Apache 或 Nginx。
-
@Fred-ii- 我使用了您在链接中放置的解决方案,它工作正常。 PS。很抱歉删除了之前的评论。
标签: php html file-upload