问题
I'm trying to edit an excel file after uploading it in laravel. the file is uploaded to local disk, so no one can access it from the public.
Filesystems.php
'local' => [
'driver' => 'local',
'root' => storage_path('app')
Routes.php
Route::get('test',function()
{
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load(Storage::disk('local')->url('margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'));
$objPHPExcel->setActiveSheetIndex(0);
echo $objPHPExcel->getActiveSheet()->getHighestRow();
})->middleware('admin');
I keep getting error:
Could not open /storage/margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx for reading! File does not exist.
this works
$objPHPExcel = PHPExcel_IOFactory::load('..'.Storage::disk('local')->url('app/margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'));
but it is annoying. I thought specifying 'root' => storage_path('app') in the filesystems.php means that Storage::disk('local') will be in the app directly
回答1:
The URL method is used to get URL for the file (ie: something to access it from the browser). What you want to use id the get()
method.
$contents = Storage::get(''margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'');
That being said, there is still easier way to manipulate uploaded files with Laravel.
// this will take the uploaded file from the request
// and store it into `/storage/someFolder`
$path = $request->file('file')->store('someFolder');
You can then use the value returned to $path
to access the file.
EDIT
After discussion below, it was decided by OP that, even if it's not the the perfect solution, he would use $objPHPExcel = PHPExcel_IOFactory::load('..'.Storage::disk('local')->url($fileLocation));
.
来源:https://stackoverflow.com/questions/43274387/laravel-how-to-access-a-file-in-the-local-disk-file-does-not-exist