php - Storing Excel file using Binary content issue
问题描述
- Laravel Version: 5.5
- PHP Version
$ php --version
: PHP PHP 7.1.3 (cli) - Database Driver & Version $
mysql --version
: mysql Ver 8.0.23-0ubuntu0.20.04.1
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.
解决方案
尝试这个
//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'));
推荐阅读
- firefox - HTML5 音频擦过缓冲区在 chrome 中有效,但在 Firefox 中无效
- android - 我的手机调用HMS Push Kit服务端API后收不到任何消息。SDK版本为4.0.3.300
- python-3.x - pyspark:为什么 stop() 函数不能正常工作?
- java - 我需要使用 ffmpeg 转换音频文件,但出现错误
- screen-size - 如何在 dm 脚本中获取可用的屏幕尺寸
- html - Flask:引导选项卡
- javascript - IE默认版本自动更改为7
- google-apps-script - 与 Excel 和 VBA 相比,Google 表格和脚本真的很慢
- apache-flink - 如何知道作业是否从保存点恢复?
- android - 如何在不使用模拟位置的情况下在 android 上欺骗位置?