首页 > 解决方案 > Storing Excel file using Binary content issue

问题描述


Problem Statement:

We are storing files on an S3 bucket and want to load them via load method.

Could not open storage/app/path/file.xlsx for reading! File does not exist.


Files and Configuration:

use Illuminate\Support\Facades\Storage;
use PHPExcel_Reader_Excel2007;


      $path = Storage::disk('s3-storage')->path('app/path/' , $filename);

      $objReader = new PHPExcel_Reader_Excel2007();
      $objReader->setReadDataOnly(true);
      $objPHPExcel = $objReader->load($path);

config/filesystems.php

        's3-storage' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'root' => '/storage',
        ],

Storage::disk('s3-storage')->get()

The get method return raw string contents.

Which will throw Error on PHPExcel load method.


Edit 1:

After storing XLSX file using Laravel Storage put() method. It is writing all the binary content in first row of file.

enter image description here

标签: phplaravellaravel-5

解决方案


尝试这个

//get the path
$path = Storage::disk('s3-storage')->url($filename);
//read the content
$contents = file_get_contents($path);
//put content
Storage::disk('local')->put('file.xls', $contents);
// then it would store a file in storage/app/file.xls.

$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);

$objPHPExcel = $objReader->load(storage_path('app/file.xls'));

推荐阅读