@jerad
To upload a file in Laravel, you can follow these steps:
1 2 3 4 5 |
<form action="/upload" method="post" enctype="multipart/form-data"> @csrf <input type="file" name="file"> <button type="submit">Upload File</button> </form> |
1
|
Route::post('/upload', 'FileController@upload'); |
1
|
php artisan make:controller FileController |
Then, add the following code to the controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace AppHttpControllers; use IlluminateHttpRequest; class FileController extends Controller { public function upload(Request $request) { $file = $request->file('file'); $fileName = $file->getClientOriginalName(); $file->move(public_path('uploads'), $fileName); return 'File uploaded successfully!'; } } |
Please note that you may need to add validation and error handling to the above code to make it more robust.