当前位置:首页 > PHP

php代码实现文件下载

2026-02-13 17:54:14PHP

使用 header() 函数实现文件下载

通过设置 HTTP 头信息强制浏览器下载文件而非直接打开。关键头信息包括 Content-TypeContent-DispositionContent-Length

$file_path = '/path/to/file.pdf';
$file_name = basename($file_path);

header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$file_name\"");
header('Content-Length: ' . filesize($file_path));

readfile($file_path);
exit;

处理大文件下载

对于大文件,使用分块读取避免内存溢出。通过 fopen()fread() 逐块输出文件内容。

php代码实现文件下载

$file_path = '/path/to/large_file.iso';
$chunk_size = 1024 * 1024; // 1MB per chunk

$handle = fopen($file_path, 'rb');
while (!feof($handle)) {
    echo fread($handle, $chunk_size);
    ob_flush();
    flush();
}
fclose($handle);
exit;

安全下载注意事项

验证文件路径防止目录遍历攻击,确保文件存在且可读。

php代码实现文件下载

$base_dir = '/secure/downloads/';
$requested_file = $_GET['file'];
$file_path = realpath($base_dir . $requested_file);

if ($file_path && strpos($file_path, realpath($base_dir)) === 0 && is_file($file_path)) {
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\"");
    readfile($file_path);
} else {
    http_response_code(404);
    echo 'File not found';
}

断点续传支持

通过 HTTP_RANGE 头实现部分内容下载,适用于大文件中断后恢复下载。

$file_path = '/path/to/resumable_file.zip';
$file_size = filesize($file_path);

if (isset($_SERVER['HTTP_RANGE'])) {
    $range = $_SERVER['HTTP_RANGE'];
    list($start, $end) = explode('-', substr($range, 6));
    $start = intval($start);
    $end = $end ? intval($end) : $file_size - 1;

    header('HTTP/1.1 206 Partial Content');
    header("Content-Range: bytes $start-$end/$file_size");
    header("Content-Length: " . ($end - $start + 1));

    $fp = fopen($file_path, 'rb');
    fseek($fp, $start);
    while (!feof($fp) && ($pos = ftell($fp)) <= $end) {
        echo fread($fp, min(1024 * 16, $end - $pos + 1));
    }
} else {
    header("Content-Length: $file_size");
    readfile($file_path);
}
exit;

动态生成内容下载

将字符串或生成的内容作为文件提供下载,无需实际存储文件。

$content = "This will be downloaded as text file";
$file_name = "example.txt";

header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename=\"$file_name\"");
header('Content-Length: ' . strlen($content));

echo $content;
exit;

每个方法适用于不同场景,基础下载使用 header()readfile(),大文件需分块处理,安全场景需验证路径,高级需求可支持断点续传或动态内容生成。

标签: 代码文件
分享给朋友:

相关文章

vue实现文件模板展示

vue实现文件模板展示

Vue 实现文件模板展示的方法 在 Vue 中实现文件模板展示通常涉及文件上传、预览和模板渲染等功能。以下是几种常见的实现方式。 使用文件上传组件 通过 Vue 的文件上传组件(如 el-uploa…

h5实现响应式代码

h5实现响应式代码

响应式设计基础 HTML5实现响应式设计主要通过媒体查询(Media Queries)、弹性布局(Flexbox)、网格布局(Grid)和视口设置(Viewport Meta Tag)等技术。核心目标…

vue分页代码实现

vue分页代码实现

Vue 分页代码实现 在 Vue 中实现分页功能通常需要结合后端 API 返回的数据和前端的分页组件。以下是一个完整的实现示例,包括分页组件的封装和与后端数据的交互。 分页组件封装 创建一个…

vue简单代码实现

vue简单代码实现

以下是一个基于 Vue 3 的简单代码实现示例,包含基础功能演示: 基础模板结构 <!DOCTYPE html> <html lang="en"> <head>…

vue实现文件导入导出

vue实现文件导入导出

Vue 文件导入 在 Vue 中实现文件导入通常通过 <input type="file"> 元素结合 FileReader API 完成。 <template> <…

vue 实现git代码对比

vue 实现git代码对比

Vue 实现 Git 代码对比 在 Vue 中实现 Git 代码对比功能,可以通过集成现有的代码对比库或调用 Git 相关 API 来实现。以下是几种常见的方法: 使用 diff2html 库 di…