php 实现文件下载
使用 header 函数实现文件下载
在 PHP 中,可以通过设置 HTTP 头信息实现文件下载功能。核心是通过 header() 函数设置 Content-Disposition 为 attachment,并指定文件名。
$file_path = '/path/to/file.pdf';
$file_name = 'custom_name.pdf';
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;
处理大文件下载
对于大文件,可以使用分块读取的方式避免内存问题:
$chunk_size = 1024 * 1024; // 1MB chunks
$handle = fopen($file_path, 'rb');
while (!feof($handle)) {
echo fread($handle, $chunk_size);
ob_flush();
flush();
}
fclose($handle);
强制下载非文本文件
对于需要强制下载的文件(如图片),需要修改 MIME 类型:
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="image.jpg"');
readfile('path/to/image.jpg');
安全注意事项
确保文件路径经过验证,防止目录遍历攻击:
$base_dir = '/safe/directory/';
$requested_file = $_GET['file'];
$real_path = realpath($base_dir . $requested_file);
if ($real_path && strpos($real_path, $base_dir) === 0) {
// 安全下载逻辑
} else {
die('Invalid file requested');
}
断点续传支持
实现 HTTP Range 请求支持:
$fp = fopen($file_path, 'rb');
$size = filesize($file_path);
$length = $size;
$start = 0;
$end = $size - 1;
header("Accept-Ranges: 0-$length");
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);
flush();
}
fclose($fp);






