如何根据 php 中的文件大小动态设置 max_execution_time

How to dynamically set max_execution_time based on file size in php

我在 Laravel 工作,使用 Laravel 文件系统将 .zip 文件从 ftp 服务器下载到本地磁盘。这些文件的大小从几 MB 到几 GB 不等。

这是我当前的代码,只要我增加 max_execution_time:

public function handle()
{
    ini_set('max_execution_time', 300);
    $destination = storage_path('app/temp/' . $this->import->unique_filename);

    $ftpDisk = Storage::disk($this->import->disk);

    $stream = $ftpDisk
        ->getDriver()
        ->readStream($this->import->filename);

    file_put_contents($destination, stream_get_contents($stream), FILE_APPEND);        
}

是否可以将下载的文件分成更小的块?或者另一个比将 max_execution_time 设置为最大值更好的选项?

另一种方法是在 ftp 服务器上提取存档并逐个文件读取内容(csv/json 和图像)。

不建议在作业执行过程中更改最大执行时间。相反,您可以将作业分派到 queue 到 运行 耗时的任务中,例如下载。

然后你可以 运行 queue worker 命令后跟 --timeout=0.

php artisan queue:listen --timeout=0