0

I have a Laravel application deployed to Google App Engine. Per GAE's architecture rules, you cannot save files to a local path. I am using PHP Spreadsheet to generate Xlsx files and download them for the user. 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. For example I can write a text file with the text sample data to my google cloud storage bucket..

Storage::disk('gcs')->put('String.txt', 'sample data');

But can't find a way to do it with my excel file. Also the application is deployed in the Flex environment so I can't use the file url structure gs://

Lyres
  • 343
  • 4
  • 14
  • You don't have any file being saved, like session or logs? Can you open/write files without creating new ones? Maybe you can use an existing file as workaround. – Felippe Duarte Oct 11 '18 at 17:55

2 Answers2

0

Found a work around streaming the file with 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;
Lyres
  • 343
  • 4
  • 14
-1

Try this way :

file_put_contents(public_path('filename.xlsx'), $writer);
Besworks
  • 4,123
  • 1
  • 18
  • 34
  • you mean `file_put_contents`? Only works locally doesn't actually write the file correctly, corrupts it. – Lyres Oct 11 '18 at 18:15
  • See [this answer](https://stackoverflow.com/a/2693092) for an explanation as to why this will not work. – Besworks May 30 '22 at 18:49