php 实现下载
使用 PHP 实现文件下载
PHP 可以通过设置 HTTP 头部信息强制浏览器下载文件,而非直接打开文件。以下是几种常见的实现方法:
基本文件下载
通过设置 Content-Disposition 头部实现文件下载:
$file = 'path/to/file.pdf'; // 文件路径
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
动态生成文件并下载
可以动态生成内容(如 CSV 或文本)并直接提供下载:
$content = "Name,Email,Phone\nJohn,john@example.com,123456789";
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="users.csv"');
echo $content;
exit;
大文件分块下载
对于大文件,可以使用分块读取避免内存问题:
$file = 'large_file.zip';
$chunk_size = 1024 * 1024; // 1MB 分块
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
$handle = fopen($file, 'rb');
while (!feof($handle)) {
echo fread($handle, $chunk_size);
ob_flush();
flush();
}
fclose($handle);
exit;
安全下载注意事项
确保下载文件时验证用户权限,避免目录遍历攻击:
$base_dir = '/safe/download/directory/';
$requested_file = $_GET['file'];
$file_path = realpath($base_dir . $requested_file);
// 验证文件是否在安全目录中
if (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);
exit;
} else {
die('Invalid file request');
}
断点续传支持
添加对 HTTP Range 请求的支持实现断点续传:
$file = 'large_video.mp4';
$fp = fopen($file, 'rb');
$size = filesize($file);
$length = $size;
$start = 0;
$end = $size - 1;
header('Accept-Ranges: bytes');
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
exit;
}
if ($range == '-') {
$c_start = $size - substr($range, 1);
} else {
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
}
$c_end = ($c_end > $end) ? $end : $c_end;
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1;
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
$buffer = 1024 * 8;
while (!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
echo fread($fp, $buffer);
ob_flush();
flush();
}
fclose($fp);
exit;






