当前位置:首页 > PHP

php下载文件实现

2026-02-27 14:53:46PHP

PHP 实现文件下载

在 PHP 中实现文件下载功能通常涉及设置正确的 HTTP 头信息并输出文件内容。以下是几种常见的实现方式:

php下载文件实现

使用 readfile() 函数

$file_path = 'path/to/your/file.pdf';
$file_name = basename($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;

使用 fread() 逐块读取大文件

对于大文件,可以分块读取以避免内存问题:

php下载文件实现

$file_path = 'path/to/large/file.zip';
$file_name = basename($file_path);
$chunk_size = 1024 * 1024; // 1MB chunks

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));

$handle = fopen($file_path, '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 ($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).'"');
    header('Content-Length: ' . filesize($file_path));
    readfile($file_path);
} else {
    header('HTTP/1.0 404 Not Found');
    echo 'File not found';
}
exit;

强制下载特定 MIME 类型

可以强制浏览器下载而不是显示某些文件类型:

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

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;

注意事项

  • 确保脚本在输出文件内容前没有输出任何其他内容(包括空格和换行符)
  • 对于大文件下载,考虑增加 PHP 执行时间限制:set_time_limit(0)
  • 在生产环境中应添加适当的权限检查和日志记录
  • 考虑使用 X-Sendfile(如 Apache 的 mod_xsendfile)来提高大文件下载性能

标签: 文件php
分享给朋友:

相关文章

vue实现文件模板展示

vue实现文件模板展示

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

php实现文件上传

php实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

实现.vue文件

实现.vue文件

创建Vue单文件组件 Vue单文件组件(.vue文件)是Vue.js框架的核心特性之一,它将模板、脚本和样式封装在一个文件中。一个典型的.vue文件结构包含三个部分:<template>、…

php实现socket

php实现socket

PHP 实现 Socket 通信 PHP 可以通过 socket 扩展实现网络通信,支持 TCP/UDP 协议。以下是实现 Socket 通信的核心步骤和代码示例。 创建 TCP 服务器 // 创建…

php 购物车实现

php 购物车实现

数据库设计 购物车功能通常需要设计数据库表存储商品和用户信息。常见的表包括products(商品表)、users(用户表)和cart(购物车表)。cart表通常包含字段:id(主键)、user_id(…

php实现聊天

php实现聊天

实现 PHP 聊天功能的方法 使用 WebSocket 和 PHP 实现实时聊天 WebSocket 是一种全双工通信协议,适合实现实时聊天功能。PHP 可以通过 Ratchet 库实现 WebSo…