Struggling to File not found at path(FileNotFoundException) in lavavel 8

Viewed 23

I'm doing a test to download the file

The script I have so far:



<a href="{{ route('export.download_response') }}">
    <button>Export(Response)</button>
</a>


class ExportController extends Controller
{
    public function download_response(){
        $filePath = Storage::path('public/test.png');
        $fileName = 'test.png';
        $mimeType = Storage::mimeType($filePath);
        $headers = [['Content-Type' => $mimeType]];
        return response()->download($filePath, $fileName, $headers);
    }
}


Route::get('/export/download_response', [ExportController::class, 'download_response'])->name('export.download_response');

I have already put the file test.png in the path storage/app/public/test.png

But there are still error messages: FileNotFoundException File not found at path: var/www/html/lav_8_filedownload/storage/app/public/test.png

full code

Any help would be greatly appreciated, thanks

1 Answers

you cannot call Storage::path() function is file is not exist. so you need to add check before Storage::exists()

<a href="{{ route('export.download_response') }}">
    <button>Export(Response)</button>
</a>


class ExportController extends Controller
{
    public function download_response(){
        if(Storage::exists('public/test.png')) {
          return response(['message'=> 'No File Found.']);
        }

        Storage::disk('s3')->exists('file.jpg')
        $filePath = Storage::path('public/test.png');
        $fileName = 'test.png';
        $mimeType = Storage::mimeType($filePath);
        $headers = [['Content-Type' => $mimeType]];
        return response()->download($filePath, $fileName, $headers);
    }
}


Route::get('/export/download_response', [ExportController::class, 'download_response'])->name('export.download_response');
Related