master
master

Reputation: 375

Laravel 5.5 read a file from the local disk

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

Upvotes: 3

Views: 18498

Answers (3)

Raskul
Raskul

Reputation: 2199

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.

Upvotes: 1

Milad Teimouri
Milad Teimouri

Reputation: 1211

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

Upvotes: 4

user9266304
user9266304

Reputation:

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

Upvotes: 6

Related Questions