[英]Saving a PHPSpreadSheet Object
I have a Laravel application deployed to Google App Engine.我有一个 Laravel 应用程序部署到 Google App Engine。 Per GAE's architecture rules, you cannot save files to a local path.
根据 GAE 的架构规则,您不能将文件保存到本地路径。 I am using PHP Spreadsheet to generate Xlsx files and download them for the user.
我正在使用PHP 电子表格生成 Xlsx 文件并为用户下载它们。 So this works locally but not in the deployed app..
所以这在本地有效,但在部署的应用程序中无效。
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('storage/SomeExcelFile.xlsx');
Then I can download the file with..然后我可以用..下载文件
return Storage::disk('public')->download('SomeExcelFile.xlsx');
But without being able to saves files locally on the production version of the application, how will I store my file?但是无法在应用程序的生产版本上本地保存文件,我将如何存储我的文件? I have my application connected to a storage bucket where I can upload files.
我将我的应用程序连接到可以上传文件的存储桶。 But I need the file path to do this, and since this file was created scriptually in the application and was never stored prior to deployment, I can't find a way to store it.
但是我需要文件路径来执行此操作,并且由于该文件是在应用程序中以脚本方式创建的,并且在部署之前从未存储过,因此我找不到存储它的方法。
As far as I found you can't write the xlsx file directly to a disk.据我发现,您不能将 xlsx 文件直接写入磁盘。 For example I can write a text file with the text
sample data
to my google cloud storage bucket..例如,我可以将带有文本
sample data
的文本文件写入我的谷歌云存储桶..
Storage::disk('gcs')->put('String.txt', 'sample data');
But can't find a way to do it with my excel file.但是找不到使用我的excel文件的方法。 Also the application is deployed in the Flex environment so I can't use the file url structure
gs://
此外,应用程序部署在 Flex 环境中,因此我无法使用文件 url 结构
gs://
Found a work around streaming the file with Symfony找到了一个使用Symfony流式传输文件的工作
$response = new StreamedResponse(
function () use ($writer) {
$writer->save('php://output');
}
);
$response->headers->set('Content-Type', 'application/vnd.ms-excel');
$response->headers->set('Content-Disposition', 'attachment;filename="Services Reformatted.xlsx"');
$response->headers->set('Cache-Control', 'max-age=0');
return $response;
试试这种方式:
file_put_contents(public_path('filename.xlsx'), $writer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.