3

I am trying to import a csv file with Laravel 5.5 from the local file location. For some reason it can't find the file on my computer however the path is correct.

$fileLocation = $request->file('file')->store('csv');
$importFile = File::file(storage_path('app/' . $fileLocation));
master
  • 374
  • 2
  • 4
  • 16

3 Answers3

6

You can use the Storage facade for this. Here is an example:

if (Storage::disk($disk)->exists($filename)) {
    return Storage::disk($disk)->get($filename);
}

throw new FileNotFoundException(sprintf('File not found: %s', $filename), 404);
4

If you don't wanna use Storage facade, you can use below code to get the file

$importFile = Illuminate\Support\Facades\File::get(storage_path('app/' . $fileLocation));

but when you store a file with csv as store argument, it will save in storage/csv folder, and you just need call storage_path($fileLocation).

be sure that your storage path is correct and for more information read here

Matthew Spence
  • 986
  • 2
  • 9
  • 27
Milad Teimouri
  • 1,141
  • 9
  • 17
0

Even though it's 5 years late, I wanna answer the question.

I have faced the same issue and I couldn't retrieve a pdf file from my Storage.

after that, I recognized the path that I'm giving to the get function is wrong.

I was giving the wrong path the get() function like:

/app/public/book/example.pdf

the correct path is:

/public/book/example.pdf

so:

$filePath = '/public/book/example.pdf';
$file = Storage::get($filePath);

the get() method will return null if the file doesn't exist.

if exist() method will return false if the file doesn't exist.

Raskul
  • 1,674
  • 10
  • 26