PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

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

 January 22, 2022     composer-php, laravel, php, phpexcel, xampp     No comments   

Issue

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


Solution

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));.



Answered By - Jean-Philippe Murray
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing