当前位置:首页 > 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);

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

相关文章

css网页制作代码

css网页制作代码

CSS网页制作基础代码 以下是一些常用的CSS代码片段,可用于网页制作的基本样式设置: 全局样式重置 * { margin: 0; padding: 0; box-sizing:…

css文件怎么制作

css文件怎么制作

创建CSS文件的基本步骤 CSS文件用于定义网页的样式,可以与HTML文件分离,便于管理和维护。以下是创建CSS文件的方法: 新建文本文件 使用任意文本编辑器(如Notepad++、VS Code…

vue本地查询代码实现

vue本地查询代码实现

Vue 本地查询代码实现 在Vue中实现本地查询功能,可以通过以下几种方式实现,具体取决于数据源和查询需求。 使用计算属性进行本地查询 计算属性是Vue中处理响应式数据查询的高效方式,适合基于现有数…

vue怎么实现文件树

vue怎么实现文件树

Vue 实现文件树的方法 使用递归组件实现文件树 递归组件是Vue中实现文件树的常见方法。通过组件自身调用自身,可以处理嵌套的目录结构。 <template> <div>…

vue实现文件模板展示

vue实现文件模板展示

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

vue实现录音文件播放

vue实现录音文件播放

实现录音文件播放的方法 在Vue中实现录音文件播放需要结合HTML5的Web Audio API或第三方库。以下是两种常见的方法: 使用HTML5 Audio元素 通过Vue动态绑定<a…