当前位置:首页 > PHP

php实现文件下载代码

2026-01-15 13:44:20PHP

实现文件下载的基本方法

使用PHP实现文件下载的核心是通过设置HTTP头部信息,强制浏览器触发下载行为而非直接显示文件内容。

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

if (file_exists($file_path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $file_name . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file_path));
    readfile($file_path);
    exit;
}

处理大文件下载

对于大文件下载,使用分块读取可以避免内存耗尽问题。

php实现文件下载代码

$file_path = 'large_file.zip';
$chunk_size = 1024 * 1024; // 1MB chunks

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

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

安全下载实现

增加安全检查防止目录遍历攻击。

php实现文件下载代码

$base_dir = '/safe/download/directory/';
$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 {
    header('HTTP/1.0 404 Not Found');
    echo 'File not found';
}
exit;

进度显示实现

通过输出缓冲和JavaScript配合实现前端进度显示。

// PHP部分
$file_path = 'large_file.iso';
$file_size = filesize($file_path);
header('Content-Length: ' . $file_size);
// ...其他头部设置...

$handle = fopen($file_path, 'rb');
while (!feof($handle)) {
    echo fread($handle, 8192);
    ob_flush();
    flush();
}
// 前端JavaScript
xhr.onprogress = function(e) {
    if (e.lengthComputable) {
        var percent = (e.loaded / e.total) * 100;
        progressBar.style.width = percent + '%';
    }
};

断点续传支持

实现Range请求支持,允许断点续传。

$file_path = 'resumable_file.mp4';
$file_size = filesize($file_path);
$start = 0;
$end = $file_size - 1;

if (isset($_SERVER['HTTP_RANGE'])) {
    $range = $_SERVER['HTTP_RANGE'];
    if (preg_match('/bytes=(\d+)-(\d+)?/', $range, $matches)) {
        $start = intval($matches[1]);
        $end = isset($matches[2]) ? intval($matches[2]) : $file_size - 1;
    }
}

header('Accept-Ranges: bytes');
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $file_size);
header('Content-Length: ' . ($end - $start + 1));

$handle = fopen($file_path, 'rb');
fseek($handle, $start);
while (!feof($handle) && ($p = ftell($handle)) <= $end) {
    echo fread($handle, min(8192, $end - $p + 1));
    ob_flush();
    flush();
}
fclose($handle);

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

相关文章

vue实现网站代码

vue实现网站代码

Vue 实现网站代码的基本步骤 安装 Vue.js 通过 npm 或 CDN 引入 Vue.js。使用 npm 安装:npm install vue。或者直接在 HTML 文件中引入 Vue CDN:…

vue代码框实现

vue代码框实现

Vue 代码框实现方法 在 Vue 中实现代码框功能通常需要结合语法高亮库或 UI 组件库。以下是几种常见实现方式: 使用第三方库 Prism.js 安装 Prism.js 及相关依赖: npm…

vue实现动态修改代码

vue实现动态修改代码

Vue 动态修改代码的实现方法 Vue 提供了多种方式实现动态修改代码的需求,以下是几种常见场景和解决方案: 动态组件加载 使用 Vue 的 <component> 标签配合 is 属性…

vue文件下载怎么实现

vue文件下载怎么实现

使用 <a> 标签下载文件 在 Vue 中可以通过动态生成 <a> 标签实现文件下载。创建一个隐藏的 <a> 标签,设置 href 为文件 URL 并添加 down…

vue长轮询代码实现

vue长轮询代码实现

Vue 长轮询实现方法 长轮询(Long Polling)是一种服务器推送技术,客户端发送请求后,服务器保持连接开放直到有新数据或超时。以下是 Vue 中实现长轮询的代码示例。 基本实现逻辑 定义一…

vue实现代码实例

vue实现代码实例

以下是一个基于 Vue 3 的完整代码实例,包含核心功能实现和常见开发场景的示例: 基础组件示例 <template> <div> <h1>{{ ti…