如何使用资源逐行写入但仍使用 Laravel 的内置存储?

How can I use a resource to write line by line but still use Laravel's built in Storage?

我想用Storage::put写一个文件。该文件可能非常大 (>100MB),所以我想利用一个流,这样我就不会盲目地将所有内容都放入内存中。

我将发出多个 API 请求,然后遍历它们的结果,所以我要取回的数据不是问题,它会被限制在合理的数量范围内.

根据documentation,我需要使用:

Storage::put('file.xml', $resource);

但是 $resource 这里会是什么?

传统上,当使用 PHP 编写文件时,我会结合使用 fopenfwritefclose 来编写 'line by line'。我正在通过循环遍历各种集合并使用各种 API 来构建文件,因此 $resourceNOT 文件指针或文件引用在文档的其他地方讨论过。

那么,如何使用流和 Laravel 的 Storage 逐行写入?

根据 Laravel 5.3 documentation,查看 自动流式传输

If you would like Laravel to automatically manage streaming a given file to your storage location, you may use the putFile or putFileAs method. This method accepts either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance and will automatically stream the file to your desire location:

您可以像这样使用流媒体上传文件

$file = $request->file('file');
$path = \Storage::putFile('photos', $file);

您的表单输入位置

<input type="file" name="file">
Storage::put('file.xml', $resource);

But what would $resource be here?

$resource是您准备通过代码写入磁盘的数据。

如果你想用循环写入文件,你必须使用 ljubadr

之前写的 Storage::append($file_name, $data);

我写了 $data,但您可以在循环内为变量使用任何您想要的名称。