在PHP项目中实现zip文件下载是常见的功能需求,既可以直接提供服务器上已有的zip文件下载,也可以动态将多个文件打包成zip后再触发下载,下面会分别介绍两种场景的实现方法。

一、直接下载服务器已有的zip文件
如果服务器上已经存在需要下载的zip文件,只需要设置正确的HTTP响应头,然后输出文件内容即可,核心是通过响应头告诉浏览器这是需要下载的文件,而不是在浏览器中直接打开。
实现步骤如下:
- 检查目标zip文件是否存在,避免下载不存在的文件导致错误
- 设置Content-Type为application/zip,标识文件类型
- 设置Content-Disposition为attachment,指定下载的文件名
- 设置Content-Length为文件大小,方便浏览器显示下载进度
- 读取文件内容并输出,最后退出脚本避免多余内容输出
对应的代码示例如下:
<?php
// 服务器上已有的zip文件路径
$zipFilePath = '/server/path/to/example.zip';
// 下载时显示的文件名
$downloadFileName = 'download_example.zip';
// 检查文件是否存在
if (!file_exists($zipFilePath)) {
header('HTTP/1.1 404 Not Found');
exit('文件不存在');
}
// 设置HTTP响应头
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $downloadFileName . '"');
header('Content-Length: ' . filesize($zipFilePath));
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
// 读取并输出文件内容
readfile($zipFilePath);
exit;
?>二、动态打包多个文件为zip后下载
如果需要将服务器上的多个文件临时打包成zip再提供下载,不需要先生成实体文件保存到服务器,可以直接在内存中生成zip内容并输出,避免产生冗余的临时文件。
这里需要使用PHP内置的ZipArchive类,该类提供了创建和操作zip压缩包的方法,使用前需要确保PHP已经开启了zip扩展。
实现步骤如下:
- 创建ZipArchive实例,打开一个内存中的zip流
- 添加需要打包的文件到zip中,可以设置文件在zip内的路径
- 关闭zip流,获取zip内容的长度
- 设置下载相关的响应头,输出zip内容
对应的代码示例如下:
<?php
// 需要打包的文件列表,键是zip内的路径,值是服务器上的实际路径
$fileList = [
'docs/readme.txt' => '/server/path/to/readme.txt',
'images/logo.png' => '/server/path/to/logo.png',
'data/config.json' => '/server/path/to/config.json'
];
// 下载时显示的文件名
$downloadFileName = 'packed_files.zip';
// 创建ZipArchive实例
$zip = new ZipArchive();
// 打开内存中的zip流,ZipArchive::CREATE表示创建新压缩包
$openResult = $zip->open('php://output', ZipArchive::CREATE);
if ($openResult !== TRUE) {
exit('无法创建zip压缩包');
}
// 遍历文件列表,添加文件到zip中
foreach ($fileList as $zipPath => $realPath) {
if (file_exists($realPath)) {
// 添加文件,第一个参数是服务器文件路径,第二个参数是zip内的路径
$zip->addFile($realPath, $zipPath);
}
}
// 关闭zip,此时内容会写入到php://output流中
$zip->close();
// 获取zip内容的长度,注意需要在关闭zip后获取,因为关闭时才会完成写入
$zipLength = ob_get_length();
// 设置HTTP响应头
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $downloadFileName . '"');
header('Content-Length: ' . $zipLength);
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
exit;
?>三、注意事项
在实际使用中需要注意以下几点:
- 如果下载的zip文件较大,避免使用
readfile一次性读取整个文件到内存,可以分块读取输出,减少内存占用 - 设置响应头之前不能有任何输出,包括空格、换行、错误信息等,否则会导致响应头设置失败
- 如果需要支持断点续传,还需要额外处理Range请求头,计算对应的文件偏移量输出内容
- 动态打包时如果添加的文件较多或者文件较大,需要注意PHP的执行时间限制,必要时可以调整
max_execution_time配置
分块读取大zip文件下载的示例代码如下:
<?php
$zipFilePath = '/server/path/to/large_example.zip';
$downloadFileName = 'large_example.zip';
if (!file_exists($zipFilePath)) {
header('HTTP/1.1 404 Not Found');
exit('文件不存在');
}
// 设置响应头
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $downloadFileName . '"');
header('Content-Length: ' . filesize($zipFilePath));
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
// 分块读取文件,每块8KB
$chunkSize = 8192;
$handle = fopen($zipFilePath, 'rb');
if ($handle) {
while (!feof($handle)) {
echo fread($handle, $chunkSize);
flush(); // 输出缓冲区内容,避免占用过多内存
}
fclose($handle);
}
exit;
?>
PHPzip文件下载ZipArchive文件压缩HTTP响应头修改时间:2026-06-05 04:16:06