信息发布→ 登录 注册 退出

Swoole怎么实现文件的异步上传和下载

发布时间:2025-09-28

点击量:
Swoole通过协程与异步I/O实现高效文件上传下载。1. 上传时利用HTTP服务器接收文件,结合协程安全写入避免阻塞;2. 下载支持本地流式发送(createDownloadStream)和远程分块代理下载;3. 需启用协程、控制内存、校验文件并清理临时文件,确保安全与性能。

Swoole 实现文件的异步上传和下载,核心在于利用其异步非阻塞 I/O 能力,结合 HTTP 服务器和协程特性来高效处理文件操作。以下是具体实现方式:

异步文件上传

使用 Swoole 的 HTTP 服务器接收客户端上传的文件,并通过 协程 + 异步写入避免阻塞主线程。

实现要点:

  • 开启 Swoole 的 HTTP 服务,监听 request 事件
  • 通过 $request->files 获取上传文件信息
  • 使用 Swoole\Coroutine\Filefile_put_contents(在协程上下文中自动异步)保存文件
  • 可结合临时文件、校验、重命名等逻辑

示例代码:

$server = new Swoole\Http\Server("0.0.0.0", 9501);
$server->set([
    'enable_coroutine' => true,
    'worker_num' => 2
]);

$server->on('Request', function ($request, $response) {
    if ($request->server['request_method'] == 'POST' && isset($request->files['upload'])) {
        $file = $request->files['upload'];
        $tmpPath = "/tmp/{$file['name']}";

        // 协程安全地异步写入
        $result = Swoole\Coroutine\File::write($tmpPath, file_get_contents($file['tmp_name']));

        if ($result) {
            $response->end(json_encode(['status' => 'success', 'path' => $tmpPath]));
        } else {
            $response->end(json_encode(['status' => 'fail']));
        }
    } else {
        $response->end('

            
            
        
');
    }
});

$server->start();

异步文件下载

通过 Swoole 提供大文件或远程文件的异步流式下载,避免内存溢出,提升并发能力。

实现方式:

  • 使用 Http\Response -> createDownloadStream() 方法(Swoole 4.8+)直接流式发送本地文件
  • 对远程文件:使用 Swoole\Coroutine\Http\Client 异步获取内容,分块写入响应
  • 设置合适的 headers(Content-Type、Content-Length、Content-Disposition)

本地文件流式下载示例:

$server->on('Request', function ($request, $response) {
    if ($request->get['action'] == 'download') {
        $filePath = '/path/to/large-file.zip';
        if (file_exists($filePath)) {
            // 自动异步流式发送
            $response->createDownloadStream($filePath, 'custom-name.zip');
        } else {
            $response->status(404);
            $response->end('File not found');
        }
    }
});

远程文件代理下载(异步中转):

$server->on('Request', function ($request, $response) {
    if ($request->get['action'] == 'proxy-download') {
        $client = new Swoole\Coroutine\Http\Client('example.com', 443, true);
        $client->setHeaders([
            'Host' => "example.com",
            'User-Agent' => 'Mozilla/5.0'
        ]);
        $client->get('/large-file.zip');

        $response->header('Content-Type', 'application/octet-stream');
        $response->header('Content-Disposition', 'attachment; filename="remote-file.zip"');

        // 分块返回
        $buffer = $client->body;
        $response->write($buffer);
        $response->end();
        $client->close();
    }
});

关键注意事项

  • 协程环境:确保启用 enable_coroutine,否则 file 操作会阻塞
  • 内存控制:大文件不要一次性读入内存,使用 fread/fwrite 分块或 createDownloadStream
  • 安全性:上传文件需校验类型、大小、重命名,防止恶意上传
  • 临时清理:上传后及时处理 tmp 文件,避免堆积
基本上就这些。Swoole 的协程机制让异步文件处理变得简单高效,合理使用能显著提升 Web 服务性能。
标签:# 事件  # 和协  # 保存文件  # 客户端  # 文件上传  # 大文件  # 临时文件  # 重命名  # 上传文件  # 流式  # 上传  # http  # input  # 异步  # js  # function  # 并发  # 主线程  # 线程  # Length  #   # if  # swoole  # stream  # proxy  # ai  # app  # json  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!