Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpSpreadsheet : How to save the spreadsheet with a Laravel storage disk?

I have generated a valid spreadsheet and I am able to save it with the documented save method :

$writer = new Xlsx($spreadsheet);
$writer->save('file.xlsx');

But how can I use the storages disks provided by Laravel ? Can I get the content of the file before writing it to the disk ?

like image 602
Neekobus Avatar asked Oct 15 '25 14:10

Neekobus


1 Answers

I figured out by myself. Since there is no $writer->getContent() method here is my workaround :

  • Save to php://output
  • Capture the file content with ob_start/ob_get_contents.
  • Finally call the regular Storage put() method on any disk !

Here is my code :

    $writer = new Xlsx($spreadsheet);

    ob_start();
    $writer->save('php://output');
    $content = ob_get_contents();
    ob_end_clean();

    Storage::disk('local')->put("myfile.xlsx", $content); 
    Storage::disk('myftp')->put("myfile.xlsx", $content); 
like image 58
Neekobus Avatar answered Oct 17 '25 08:10

Neekobus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!