FileNotFoundException even if it exists in Laravel

437 Views Asked by At

In laravel, I am making an application that uploads a file and the user can retrieve that same file.

But each time I click to upload I get this error.

FileNotFoundException in FilesystemAdapter.php line 91:
/storage/ap/payments/98AcresResort_25.pdf

my controller code is this:

$filePath = "/storage/ap/payments/".$payment->payment_image;
$file = storage::disk('public')->get($filePath);

my file is located at /public/storage/ap/payments/filename.pdf

I tried php artisan storage:link

Can anyone suggest any solution, please?

2

There are 2 best solutions below

2
Clément Baconnier On BEST ANSWER

Since your file is located at this place /your-app/public/storage/ap/payments/filename.pdf you need to add a disk.

In the file config/filesystems.php

add the disk

'disks' => [

    'web' => [
        'driver' => 'local',
        'root' => public_path(),
    ],

Then you can use it like this

$filePath = "storage/ap/payments/".$payment->payment_image;
$file = Storage::disk('web')->get($filePath);

It will look for the location

/your-app/public/storage/ap/payments/filename.pdf"

But I suspect that your file /public/storage/ap/payments/filename.pdf was also in the /storage folder and that you're working through a symlink. The correct ways would be to have app instead of ap and to use the public disk like this Storage::disk('public')->path('payments/filename.pdf')

Even better, since it's a a payment, you probably don't want the file to be public and accessible to all. In that case you can use the disk local and serve the file on request. But that's maybe more advanced stuff.

0
AddWeb Solution Pvt Ltd On

You should try this:

Please run below command for create storage folder in public foler:

php artisan storage:link


$filePath = public_path("storage/ap/payments/".$payment->payment_image);