当前位置:首页 > PHP

php实现批量下载

2026-02-13 19:08:14PHP

PHP实现批量下载的方法

使用ZipArchive类打包下载

创建临时压缩包,将多个文件打包后提供下载链接。示例代码:

$zip = new ZipArchive();
$zipName = 'downloads.zip';

if ($zip->open($zipName, ZipArchive::CREATE) === TRUE) {
    $files = ['file1.pdf', 'file2.jpg', 'file3.docx'];

    foreach ($files as $file) {
        if (file_exists($file)) {
            $zip->addFile($file);
        }
    }

    $zip->close();

    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="'.basename($zipName).'"');
    header('Content-Length: ' . filesize($zipName));
    readfile($zipName);
    unlink($zipName); // 删除临时文件
}

多文件流合并下载

通过HTTP头控制实现多个文件合并下载输出:

php实现批量下载

$fileList = [
    '/path/to/file1.txt',
    '/path/to/file2.txt'
];

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="combined_files.bin"');

foreach ($fileList as $file) {
    if (file_exists($file)) {
        readfile($file);
    }
}

异步队列处理大文件批量下载

对于大型批量下载任务,建议采用队列系统:

php实现批量下载

// 生产者部分(添加下载任务到队列)
$downloadJobs = [
    ['url' => 'http://example.com/file1', 'saveAs' => 'local1.ext'],
    ['url' => 'http://example.com/file2', 'saveAs' => 'local2.ext']
];

foreach ($downloadJobs as $job) {
    file_put_contents('download_queue.txt', json_encode($job)."\n", FILE_APPEND);
}

// 消费者部分(后台处理)
while (true) {
    $queue = file('download_queue.txt');
    if (!empty($queue)) {
        $job = json_decode(array_shift($queue), true);
        file_put_contents($job['saveAs'], file_get_contents($job['url']));
        file_put_contents('download_queue.txt', implode('', $queue));
    }
    sleep(5);
}

注意事项

  • 设置合理的执行时间限制:set_time_limit(0)
  • 处理内存限制:ini_set('memory_limit', '1024M')
  • 对于远程文件下载,考虑使用cURL代替file_get_contents
  • 添加权限验证防止未授权访问
  • 记录下载日志用于审计追踪

安全增强措施

验证文件路径防止目录遍历攻击:

function safeDownload($filePath) {
    $baseDir = '/secure/download/directory/';
    $realPath = realpath($baseDir . $filePath);

    if (strpos($realPath, $baseDir) === 0 && file_exists($realPath)) {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($realPath).'"');
        readfile($realPath);
    } else {
        header('HTTP/1.0 404 Not Found');
    }
}

标签: 批量php
分享给朋友:

相关文章

php实现验证码

php实现验证码

PHP实现验证码的方法 使用GD库生成验证码 GD库是PHP中处理图像的扩展,可用于生成验证码图片。 <?php session_start(); $width = 120; $height…

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面…

php 实现文件下载

php 实现文件下载

实现文件下载的基本方法 使用 PHP 实现文件下载的核心是通过设置 HTTP 头部信息,强制浏览器将文件作为附件下载而非直接显示。以下是一个基础实现示例: $file_path = '/path/t…

php实现文件的下载

php实现文件的下载

PHP 实现文件下载的方法 在 PHP 中实现文件下载功能通常需要设置正确的 HTTP 头信息,并输出文件内容。以下是几种常见的实现方式: 使用 header() 函数强制下载 通过设置 Cont…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数:…

php实现上传图片

php实现上传图片

上传图片的基本流程 PHP 实现图片上传功能需要处理文件接收、验证、保存等步骤。以下是具体实现方法。 创建 HTML 表单 在 HTML 中创建一个表单,设置 enctype="multipart…