게시글 등록 후 첨부파일을 다운로드하는 기능을 구현할 일이 생겼다.
//view
<h2>Files:</h2>
@foreach ($files as $file)
<p>
{{-- {{ $file }} --}}
<a href="{{ route('products.downloadFile',['foldername' => $product->name, 'filename' => basename($file)]) }}">
{{ basename($file) }}
</a>
</p>
@endforeach
여기서 file)로 넘겼다.
//Route
Route::get('/downloadFile/{foldername}/{filename}', [ProductController::class, 'downloadFile'])->name('products.downloadFile');
//Controller
public function downloadFile($foldername, $filename)
{
$filePath = 'docs/' . $foldername . '/' . $filename;
if (Storage::exists($filePath)) {
return response()->download(storage_path('app/' . $filePath));
}
abort(404, 'File not found');
}
아 이게 중간에 구현하면서
$filePath = 'docs/' . $foldername . '/' . $filename;
를
$filePath = 'app/docs/' . $foldername . '/' . $filename;
로 했는데 제대로 동작을 안해서 애먹었는데
어떤식으로 동작을 안했냐면
계속 이동되면서 404 NOT FOUND 가 떴다.
그래서 $filePath 를 건들지 말고
download(storage_path('app/' . $filePath));
에서 'app/' 이런식으로 추가하니까 잘됐다.

잘됨 성공적으로 구현 끝