I'm using Laravel 5.2 and I want to make a form which can upload a pdf file with it. I want to add that file on folder "files" in "public" folder.
here is my view:
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
and what should I do next? what should I add in my controller and route?
You Could Use Simple Method It Can Save The File
For More Information Here
You can take a look at how i upload files, all files are accepted: first the code for the create.blade.php form
Remember to set files to true
Then the uploadspanel.create_form
then the controller store function
First you should add
enctype="multipart/form-data"
to your<form>
tag. Then in your controller handle the file upload as follow:Link to Laravel Docs for Handling File Uploads
Laravel casts the file type params in request to
UploadedFile
objects. You can see Symfony'sUploadedFile
class here for available methods and attributes.First of all, the documentation tells you exactly what to do here.
What you want to do is adding this to your
<form>
tag:enctype="multipart/form-data"
(This allows you to upload data), set amethod
(get/post) and anaction
(url).Then you want to set up your routes.
For example:
Route::post('/pdf/upload', 'FileController@upload');
This way you make sure that when you send the form it will go to your
FileController
withupload
as function.In your controller you want to declare the file as explained in the docs.
$file = $request->file('photo');
.From this point you can do whatever you'd like to do with the file (
$file
). For example uploading it to your own server.you can this code for upload file in Laravel: