【问题标题】:Storing Excel file using Binary content issue使用二进制内容问题存储 Excel 文件
【发布时间】:2021-07-29 13:17:04
【问题描述】:
  • Laravel 版本:5.5
  • PHP 版本$ php --version:PHP PHP 7.1.3 (cli)
  • 数据库驱动和版本 $mysql --version: mysql Ver 8.0.23-0ubuntu0.20.04.1

问题陈述:

我们将文件存储在 S3 存储桶上,并希望通过 load 方法加载它们。

无法打开 storage/app/path/file.xlsx 进行阅读!文件不存在。


文件和配置:

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()

get 方法返回原始字符串内容。

这将在 PHPExcel load 方法上引发错误。


编辑 1:

使用 Laravel Storage put() 方法存储 XLSX 文件后。它正在将所有二进制内容写入文件的第一行。

【问题讨论】:

  • 如果您在 S3 文件 ($path) 上使用 file_get_contents 并将其二进制文件保存为 xls 或 xlsx 文件使用 file_put_contents,获取文件路径然后使用 PHPExcel 加载它会怎样?跨度>
  • @AuliaWiguna 你能展示一下它的实现吗?

标签: php laravel laravel-5


【解决方案1】:

试试这个

//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'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2015-07-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多