You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HTTP Header下载文件时不显示文件大小及断点续传选项问题

问题:PHP下载脚本在WHM/cPanel(搭载Engintron)环境下无法显示文件大小及断点续传

我编写了一段PHP下载脚本,实现类似https://example.com/dl.php?file=test.zip的下载链接逻辑:

  • 脚本会校验HTTP Referer,仅当来源在允许列表内时,才允许从up目录下载文件
  • 通过URL参数file获取文件名,校验文件合法性后执行下载

目前基础功能正常,但在全新安装的WHM/cPanel+Engintron环境中,文件下载时不显示文件大小,也无法使用断点续传(Resume)功能。相同脚本在AApanel环境下运行完全正常,求解决办法。

dl.php代码

<?php
// 允许的来源域名
$allowedDomains = [
    'https://reidostorrents.net.br/',
    'https://programastorrent.com.br/',
    'https://fullprogramindir.com.tr/',
    'https://jogosbaixar.net/',
    'https://gustavortech.org/',
    'https://ativadorfiles.com/',
    'https://downloadcomputerprograms.org/',
    'https://baixakiprogramas.com.br/',
    'https://computergamesegypt.com/'
];

// 获取HTTP Referer来源
$referer = $_SERVER['HTTP_REFERER'];

// 校验来源是否在允许列表内
if (in_array($referer, $allowedDomains) || startsWith($referer, 'https://reidostorrents.net.br')) {
    // 指定文件目录
    $directory = __DIR__ . '/up'; 

    // 获取请求的文件名
    $requestedFile = isset($_GET['file']) ? $_GET['file'] : '';

    // 校验文件扩展名
    $allowedExtensions = ['zip', 'rar'];
    $fileExtension = pathinfo($requestedFile, PATHINFO_EXTENSION);
    $file = $directory . '/' . basename($requestedFile);

    if (is_file($file) && in_array($fileExtension, $allowedExtensions)) {
        // 处理断点续传逻辑
        $fileSize = filesize($file);

        // 处理范围请求
        $range = isset($_SERVER['HTTP_RANGE']) ? $_SERVER['HTTP_RANGE'] : '';
        if ($range) {
            list($unit, $range) = explode('=', $range, 2);
            if ($unit == 'bytes') {
                list($start, $end) = explode('-', $range, 2);
                $start = (int)$start;
                $end = (isset($end) && is_numeric($end)) ? (int)$end : $fileSize - 1;

                if ($start > $end || $start >= $fileSize || $end >= $fileSize) {
                    header('HTTP/1.1 416 Requested Range Not Satisfiable');
                    header("Content-Range: bytes */$fileSize");
                    exit;
                }
            } else {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                exit;
            }
        } else {
            $start = 0;
            $end = $fileSize - 1;
        }

        $length = $end - $start + 1;

        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Content-Length: ' . $length);
        header('Accept-Ranges: bytes');

        if ($range) {
            header('HTTP/1.1 206 Partial Content');
            header("Content-Range: bytes $start-$end/$fileSize");
        } else {
            header('HTTP/1.1 200 OK');
        }

        $fileHandle = fopen($file, 'rb');
        fseek($fileHandle, $start);

        $bufferSize = 8192;
        while (!feof($fileHandle) && ($pos = ftell($fileHandle)) <= $end) {
            if ($pos + $bufferSize > $end) {
                $bufferSize = $end - $pos + 1;
            }
            echo fread($fileHandle, $bufferSize);
            ob_flush();
            flush();
        }

        fclose($fileHandle);
    } else {
        // 文件不存在或扩展名不允许
        http_response_code(404);
        echo 'No File Found.';
    }
} else {
    // 来源不允许,返回403并跳转
    http_response_code(403);
    header('Location: https://reidostorrents.net.br');
    exit;
}

// 辅助函数:检查字符串是否以指定前缀开头
function startsWith($haystack, $needle) {
    return strpos($haystack, $needle) === 0;
}
?>

.htaccess代码

# 启用重写引擎
RewriteEngine On

# 允许访问所有文件
<Files *>
    Order Allow,Deny
    Allow from all
</Files>

# 禁用目录列表
Options -Indexes

内容的提问来源于stack exchange,提问作者Ahtsham Khan Qazi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:23:13