首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >简单文件下载器

简单文件下载器
EN

Stack Overflow用户
提问于 2012-03-13 15:56:04
回答 2查看 572关注 0票数 2

我是php的新手,通过阅读SO上列出的一些问题,我成功地将这个简单的下载脚本串在了一起,并想请那些更熟悉php的人看看下面的代码,看看我的实现是否有任何明显的缺陷或任何应该改变的地方。

只想让你知道,在我有限的测试期间,一切似乎都很好,但正如我所说的,我是php的新手,希望确保我不会遗漏一些可能会在以后破坏脚本的东西。

代码语言:javascript
运行
复制
<?php

//Settings
$filesPath   = './files';
$fileName    = $_GET['file'];
$allowedExts = array('jpg','png','gif');


//Functions 
//Returns the extension portion of a filename.
function file_extension($fileName)
{
    $path_info = pathinfo($fileName);
    return strtolower($path_info['extension']);
}

//Validation and processing
//Check that a file is actually being requested
if (empty($fileName)) {
    die('no file was requested');
}

//Check that the file is allowed to be downloaded   
if (!in_array(file_extension($fileName), $allowedExts)) {
    die('you cannot download this file');
}

//Get the file  
if (file_exists($filesPath . DIRECTORY_SEPARATOR . $fileName)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($fileName));
    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($fileName));
    ob_clean();
    flush();
    readfile($fileName);
    exit;
}
?>

蒂娅,戴夫

EN

回答 2

Stack Overflow用户

发布于 2012-03-13 16:01:20

您的脚本可能容易受到目录遍历的攻击。在您的示例中,我将对文件名使用realpath(),并检查它是否为.files/中的有效文件。

有人有可能遍历目录树并窃取/etc/passwd等文件。

票数 3
EN

Stack Overflow用户

发布于 2012-03-13 17:16:33

你是开放的黑客攻击,有人可以只做?文件=../etc/passwd/和bam拥有你最好做一些检查,如何防止lfi

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9680204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档