hhxsv5 / laravel-s

LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.
MIT License
3.82k stars 471 forks source link

streamDownload下载大文件总是内存超限 #425

Open jasonbigl opened 1 year ago

jasonbigl commented 1 year ago

+---------------------------+---------+ | Component | Version | +---------------------------+---------+ | PHP | 7.4.21 | | Swoole | 4.8.12 | | LaravelS | 3.7.31 | | Laravel Framework [local] | 8.83.13 | +---------------------------+---------+

重现代码如下。

这是为什么?laravel streamDownload是用来下载大文件的,怎么和laravels+swoole配合就回超过内存限制?找了大半天不知道原因在哪里

php配置的内存限制是128M,请问应该怎么解决呢?(除了修改内存限制)

<?php
namespace App\Http\Controllers;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;

class IndexController extends Controller
{
  public function export(Request $request){
    return response()->streamDownload(function () {
            $file = fopen('php://output', 'w+');

            $i=1;
            while ($i <= 1000000) {
                $data = Str::random(mt_rand(100, 200));
                fputcsv($file, [$data]);
                $i++;
            }

            Log::info(memory_get_usage() / 1024 / 1024);

            fclose($file);
        }, 'file.csv');
  }

}