I have a project in laravel where I have to take an pdf file, rename it and saves it in the storage. It should then redirect back. With another function it shoud show this pdf file in the browser in another tap.
I am not getting an error but it is not saving my file
**This is the controller:**
namespace AppHttpControllers;
use IlluminateHttpRequest;use IlluminateSupportFacadesStorage;use IlluminateRoutingController;
class FileController extends Controller
{
public function index()
{
return view('file-upload');
}
public function upload(Request $request)
{
$request->validate([
'file' => 'required|mimes:pdf|max:2048',
]);
$file = $request->file('pdf');
if (Storage::exists('public/test.pdf')) {
Storage::disk('public')->delete('test.pdf');
$file->storeAs('public', 'test.pdf');
} else {
$file->storeAs('public', 'test.pdf');
}
return redirect()->back();
}
public function show()
{
$path = public_path('storage/test.pdf');
return response()->file($path);
}
}
2
Answers
I changed file in the validation to pdf and redirected the show function to the
<iframe>
view and everything works well now.And the show function:
If you use your storage path then run arisan command