Laravel: how to access a file in the local disk? - File does not exist

被刻印的时光 ゝ 提交于 2021-01-29 04:32:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!