通过设置HTTP响应头可强制浏览器下载文件并支持断点续传。使用Content-Disposition: attachment触发下载,结合Range请求头实现断点续传,通过解析HTTP_RANGE计算起始位置,返回206状态码及Content-Range信息,分块读取文件避免内存溢出,同时建议将文件存于Web目录外、校验权限、过滤文件名以确保安全。

在Web开发中,有时需要让用户下载服务器上的文件,而不是在浏览器中直接打开。PHP可以通过设置合适的HTTP响应头来实现强制浏览器下载文件,同时还可以支持断点续传功能,提升大文件下载体验。
默认情况下,浏览器可能会尝试在内部打开某些文件类型(如PDF、图片、文本等)。通过设置Content-Disposition: attachment,可以强制浏览器将文件作为下载处理。
示例代码:
$file_path = 'path/to/your/file.pdf';
$file_name = 'download.pdf';
<p>if (file_exists($file_path)) {
// 清除缓冲区,防止输出干扰
ob_clean();
flush();</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">// 设置响应头
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Content-Length: ' . filesize($file_path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
// 读取并输出文件内容
readfile($file_path);
exit;} else { http_response_code(404); echo "文件未找到。"; }
关键说明:
立即学习“PHP免费学习笔记(深入)”;
Content-Type: application/octet-stream 表示任意二进制流,浏览器不会尝试解析。Content-Disposition: attachment 触发下载对话框。ob_clean()和flush()避免因前面输出导致下载失败。对于大文件,用户可能因网络中断需要恢复下载。HTTP协议通过Range请求头支持断点续传,PHP可以通过解析该头信息返回部分内容。
支持断点续传的完整示例:
function download_with_resume($file_path, $file_name) {
if (!file_exists($file_path)) {
http_response_code(404);
echo "文件不存在。";
return;
}
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">$size = filesize($file_path);
$start = 0;
$end = $size - 1;
$length = $size;
// 检查是否有 Range 请求头
if (isset($_SERVER['HTTP_RANGE'])) {
preg_match('/bytes=(\d+)-(\d*)/', $_SERVER['HTTP_RANGE'], $matches);
$start = intval($matches[1]);
if (!empty($matches[2])) {
$end = intval($matches[2]);
}
// 计算本次传输长度
$length = $end - $start + 1;
// 发送 206 Partial Content 状态码
http_response_code(206);
header("Content-Range: bytes $start-$end/$size");
}
// 发送标准头
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Accept-Ranges: bytes');
header("Content-Length: $length");
header("Content-Transfer-Encoding: binary");
// 打开文件,跳转到起始位置
$fp = fopen($file_path, 'rb');
fseek($fp, $start);
// 分段输出,避免内存溢出
while ($length > 0 && !feof($fp)) {
$read_size = min($length, 8192);
$data = fread($fp, $read_size);
echo $data;
flush();
$length -= strlen($data);
}
fclose($fp);
exit;}
// 调用函数 download_with_resume('path/to/large-file.zip', 'large-file.zip');
断点续传要点:
HTTP_RANGE头判断是否为断点请求。206 Partial Content状态码表示部分响应。Content-Range告知客户端数据范围。直接暴露文件路径存在风险,建议采取以下措施:
../)。基本上就这些。实现文件下载不复杂,但要兼顾兼容性、性能和安全。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号