php 做一个文件下载服务器,得避免跨路径工具,安全很重要

任务

当使用PHP编写一个文件下载服务器时,安全性是非常重要的。以下是一些基本的步骤和最佳实践,以确保您的文件下载服务器是安全的,并且与CDN加速友好:

基本要求:

验证和授权:
确保只有经过授权的用户才能下载文件。您可以使用会话(sessions)或令牌(tokens)来验证用户身份。

输入验证:
永远不要信任用户输入。验证和清理任何来自用户的输入,以防止路径遍历攻击(例如,目录遍历或本地文件包含)。

文件存在性和可访问性检查:
在提供文件下载之前,检查文件是否存在并且用户有权访问它。

文件类型和大小限制:
限制可下载文件的类型和大小,以减少潜在的安全风险。

使用HTTP头部设置:
设置适当的HTTP头部来告诉浏览器如何处理文件,例如Content-Type、Content-Disposition和Content-Length。

X-Sendfile/X-Accel-Redirect:
如果您的服务器支持(如Nginx和某些配置的Apache),使用X-Sendfile或X-Accel-Redirect头部可以将文件发送任务委托给服务器,而不是通过PHP来读取和发送文件。这可以大大提高性能,并减少PHP的内存消耗。

日志和监控:
记录所有下载活动,并设置警报以监控任何异常或可疑活动。

使用HTTPS:
确保您的网站使用HTTPS,以保护用户数据和下载链接不被中间人攻击。

对CDN友好:
    设置适当的缓存头部,如Cache-Control和Expires,以允许CDN缓存文件。
    如果可能,将文件直接托管在CDN上,并通过CDN的API或配置来管理文件的可访问性和版本。
    考虑使用CDN的URL签名功能来确保只有授权的用户才能访问CDN上的文件。

错误处理:
当发生错误时(如文件不存在或用户未授权),返回适当的HTTP状态码(如404或403),并不要泄露任何敏感信息。

Nginx如何配置使用X-Sendfile或X-Accel-Redirect头部可以将文件发送任务委托给服务器

当使用Nginx作为Web服务器时,可以使用X-Sendfile(在某些系统上)或X-Accel-Redirect头部来将文件发送任务委托给Nginx本身,而不是由应用程序(如PHP)直接发送文件。这可以提高性能和安全性,因为Nginx可以更有效地处理文件传输。

然而,需要注意的是,X-Sendfile并不是Nginx的内置功能,而是某些系统上(如Lighttpd)的一个特性。但在Nginx中,我们通常使用X-Accel-Redirect来实现类似的功能。

以下是如何在Nginx中配置使用X-Accel-Redirect的步骤:

  1. 定义内部位置
    在Nginx配置文件中,定义一个内部位置(location),只有Nginx本身可以访问这个位置。这将确保用户不能直接通过URL访问这些文件。
location /internal/ {
    internal;
    alias /path/to/your/protected/files/;
}

在这个例子中,/internal/是一个内部位置,而/path/to/your/protected/files/是实际文件存储的路径。
2. 在应用程序中设置X-Accel-Redirect头部
当应用程序(如PHP)想要发送一个文件时,它应该设置一个X-Accel-Redirect头部,其值指向之前定义的内部位置的相对路径。

例如,在PHP中:

header('X-Accel-Redirect: /internal/yourfile.zip');
exit;

这将告诉Nginx从/path/to/your/protected/files/yourfile.zip发送文件,而不是由PHP来读取和发送文件。
3. 确保应用程序无法直接访问文件
为了防止应用程序(如PHP)直接访问或列出受保护的文件,您应该确保Nginx配置中的相关位置(location)不允许直接访问这些文件。这通常是通过设置deny all;来实现的。
4. 重新加载或重启Nginx
在修改了Nginx配置文件后,您需要重新加载或重启Nginx以使更改生效。这通常可以通过运行nginx -s reloadsystemctl reload nginx(取决于您的系统和Nginx的安装方式)来完成。
5. 测试配置
最后,确保测试您的配置以确保它按预期工作。尝试通过应用程序请求一个文件,并检查Nginx是否正确地发送了该文件,同时确保用户无法直接通过URL访问这些文件。

最后的代码示例

<?php  
ob_start(); // 开启输出缓冲  

// 设置文件目录  
$directory = '/d/downloads/webdav/data/dw';  
  
// 检查目录是否存在且可读  
if (is_dir($directory) && is_readable($directory)) {  
    // 获取目录下的文件列表  
    $files = scandir($directory);  
      
    // 遍历文件列表,排除.和..  
    foreach ($files as $file) {  
        if ($file != '.' && $file != '..') {  
            // 构造Nginx内部位置的URL  
            $internalUrl = '/internal/' . $file;  
              
            // 输出下载链接,点击时PHP将设置X-Accel-Redirect头部  
            echo '<a href="?download=' . htmlspecialchars($file) . '">' . htmlspecialchars($file) . '</a><br>';  
        }  
    }  
} else {  
    echo 'Directory not found or not readable.';  
}  
  
// 检查是否有下载请求  
if (isset($_GET['download'])) {  
    // 获取要下载的文件名  
    $file = basename($_GET['download']); 
    echo $file;
      
    // 验证文件名,防止目录遍历等攻击  
    // 这里只是一个简单的示例,你可能需要更复杂的验证逻辑  
    if (preg_match('/^[a-zA-Z0-9_\.-]+$/', $file) && file_exists($directory . '/' . $file))
    {  
        // 设置X-Accel-Redirect头部  
        header('X-Accel-Redirect: /internal/' . $file);  
          
        // 其他必要的头部信息  
        header('Content-Type: application/octet-stream');  
        header('Content-Disposition: attachment; filename="' . $file . '"');  
          
        // 确保不输出任何内容  
        exit;  
    } else {  
        // 无效的文件名  
        http_response_code(404);  
        echo 'File not found.';  
        echo $_GET['download'];
    }  
}  
ob_end_flush(); // 清理并发送输出
?>

最后的效果

包含两个文件,一个dw.php作为显示,一个dwprocess.php 用于下载。
在这里插入图片描述

dw.php

<!DOCTYPE html>  

<html lang="en">  

<head>  

<meta charset="UTF-8">  

<meta name="viewport" content="width=device-width, initial-scale=1.0">  

<title>数据回传文件下载</title>  

<style>


ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 10%;
    background-color: #f1f1f1;
    position: fixed;
    height: 100%;
    overflow: auto;
}

li a {
    display: block;
    color: #000;
    padding: 8px 16px;
    text-decoration: none;
}

li a.active {
    background-color: #4CAF50;
    color: white;
}

li a:hover:not(.active) {
    background-color: #555;
    color: white;
}





  /* 基本样式 */  

  body {  

    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;  

    margin: 0;  

    padding: 20px;  

  }  

  

  /* 表格样式 */  

  table {  

    width: 100%;  

    border-collapse: collapse;  

    border-spacing: 0;  

    border: none;  

  }  

  

  th, td {  

    padding: 10px;  

    text-align: left;  

    border-bottom: 1px solid #ddd; /* 灰色行分隔线 */  

  }  

  

  th {  

    background-color: #f5f5f5; /* 头部背景色 */  

    font-weight: bold;  

  }  

  

  tr:nth-child(even) {  

    background-color: #f9f9f9; /* 偶数行背景色 */  

  }  

  

  /* 下载链接样式 */  

  a {  

    color: #007BFF; /* 链接颜色 */  

    text-decoration: none;  

  }  

  

  /* 可选:表头圆角 */  

  thead tr:first-child th:first-child {  

    border-top-left-radius: 10px;  

  }  

  

  thead tr:first-child th:last-child {  

    border-top-right-radius: 10px;  

  }  

  

  /* 可选:表格底部圆角(如果表格不是整页内容) */  

  /* tbody tr:last-child td:first-child {  

    border-bottom-left-radius: 10px;  

  }  

  

  tbody tr:last-child td:last-child {  

    border-bottom-right-radius: 10px;  

  } */  

</style>
</head>  

<body>


<ul>
  <li><a class="active" href="#">数据回传下载</a></li>
</ul>

<div style="margin-left:15%;padding:1px 16px;height:1000px;">
    <br>
数据回传的下载列表,采用了规则校验,只是要英文字符、数字、下划线-。注意文件名不要使用中文字符。</br></br></br>
<?php  
ob_start(); // 开启输出缓冲  

// 设置文件目录  
$directory = '/d/downloads/webdav/data/dw';  
  
// 检查目录是否存在且可读  
if (is_dir($directory) && is_readable($directory)) {  

    // 获取目录下的文件列表  

    $files = scandir($directory);  

  

    // 开始表格  

    echo '<table border="1">';  

    echo '<tr><th>数据回传的文件名</th><th>大小 (KB)</th><th>修改时间</th><th>类型</th></tr>';  

  

    // 遍历文件列表,排除.和..  

    foreach ($files as $file) {  

        if ($file != '.' && $file != '..') {  

            // 构造文件路径  

            $filePath = $directory . '/' . $file;  

  

            // 获取文件信息  

            $fileSize = filesize($filePath) / 1024; // 转换为KB  

            $fileTime = date('Y-m-d H:i:s', filemtime($filePath)); // 格式化时间戳  

  

            // 尝试获取文件类型,mime_content_type()在某些系统上可能不可用  

            $fileType = mime_content_type($filePath) ?: 'Unknown';  

  

            // 如果mime_content_type()不可用,使用pathinfo()来猜测文件扩展名  

            if ($fileType == 'Unknown') {  

                $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);  

                $fileType = '.' . $fileExtension == '' ? 'Unknown' : 'Type of ' . $fileExtension;  

            }  

  

            // 输出文件信息到表格  

            echo '<tr>';  

            // 在这里添加下载链接  

            echo '<td><a href="/dwprocess.php?download=' . htmlspecialchars($file) . '">' . htmlspecialchars($file) . '</a></td>';  

            echo '<td>' . number_format($fileSize, 2) . '</td>';  

            echo '<td>' . htmlspecialchars($fileTime) . '</td>';  

            echo '<td>' . htmlspecialchars($fileType) . '</td>';  

            echo '</tr>';  

        }  

    }  

  

    // 结束表格  

    echo '</table>';  

}else {  
    echo 'Directory not found or not readable.';  
}  

ob_end_flush(); // 清理并发送输出
?>

</div>
</body>  

</html>

dwprocess.php

<?php  
ob_start(); // 开启输出缓冲  

// 设置文件目录  
$directory = '/d/downloads/webdav/data/dw';  
  

// 检查是否有下载请求  
if (isset($_GET['download'])) {  
    // 获取要下载的文件名  
    $file = basename($_GET['download']); 
    echo $file;
      
    // 验证文件名,防止目录遍历等攻击  
    // 这里只是一个简单的示例,你可能需要更复杂的验证逻辑  
       
    if (preg_match('/^[a-zA-Z0-9_.\-()]+$/', $file) && file_exists($directory . '/' . $file))
    {  
        // 设置X-Accel-Redirect头部  
        header('X-Accel-Redirect: /internal/' . $file);  
          
        // 其他必要的头部信息  
        header('Content-Type: application/octet-stream');  
        header('Content-Disposition: attachment; filename="' . $file . '"');  
          
        // 确保不输出任何内容  
        exit;  
    } else {  
        // 无效的文件名  
        http_response_code(404);  
        echo 'File not found.';  
        echo $_GET['download'];
    }  
}  
ob_end_flush(); // 清理并发送输出
?>

增加了文件排序功能

<?php  
ob_start(); // 开启输出缓冲  

// 设置文件目录  
$directory = '/d/downloads/webdav/data/dw';  
  
// 检查目录是否存在且可读  
if (is_dir($directory) && is_readable($directory)) {  

    // 获取目录下的文件列表  

    $files = scandir($directory);  

// 初始化一个数组来存储文件名和对应的修改时间  
$filesWithTime = [];  
  
// 遍历文件列表,排除.和..,并收集文件名和修改时间  
foreach ($files as $file) {  
    if ($file != '.' && $file != '..') {  
        $filePath = $directory . '/' . $file;  
        $fileTime = filemtime($filePath); // 获取时间戳,以便进行排序  
        $filesWithTime[$file] = $fileTime; // 关联数组,键为文件名,值为修改时间戳  
    }  
}  
  
// 使用uasort对关联数组进行排序,基于值(即修改时间戳)  
uasort($filesWithTime, function($a, $b) {  
    return $b - $a; // 升序排序  
    // 如果需要降序排序,可以返回 $b - $a;  
});  
  
// 开始表格  
echo '<table border="1">';  
echo '<tr><th>数据回传的文件名</th><th>大小 (KB)</th><th>修改时间</th><th>类型</th></tr>';  
  
// 遍历排序后的数组  
foreach ($filesWithTime as $file => $time) {  
    $filePath = $directory . '/' . $file;  
  
    // 获取文件信息  
    $fileSize = filesize($filePath) / 1024; // 转换为KB  
    $fileTimeFormatted = date('Y-m-d H:i:s', $time); // 格式化时间戳  
  
    // 尝试获取文件类型  
    $fileType = mime_content_type($filePath) ?: 'Unknown';  
  
    // 如果mime_content_type()不可用,使用pathinfo()来猜测文件扩展名  
    if ($fileType == 'Unknown') {  
        $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);  
        $fileType = '.' . $fileExtension == '' ? 'Unknown' : 'Type of ' . $fileExtension;  
    }  
  
    // 输出文件信息到表格  
    echo '<tr>';  
    echo '<td><a href="/dwprocess.php?download=' . htmlspecialchars($file) . '">' . htmlspecialchars($file) . '</a></td>';  
    echo '<td>' . number_format($fileSize, 2) . '</td>';  
    echo '<td>' . htmlspecialchars($fileTimeFormatted) . '</td>';  
    echo '<td>' . htmlspecialchars($fileType) . '</td>';  
    echo '</tr>';  
}  
  
// 结束表格  
echo '</table>';


}else {  
    echo 'Directory not found or not readable.';  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hali_Botebie

文中错误请不吝指正!!!!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值