Reputation: 333
I'm new to Laravel 5.1
Can you guys help me on how to upload files like docx
, PDF
or image
to store it in the database using Laravel 5.1
?
I browsed a lot of tutorials but not in Laravel 5.1
I'm trying it myself but it didn't work.
NotFoundHttpException in
C:\Loucelle\ _\ _\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php line 161:
Can you help me by giving any sample codes?
Upvotes: 3
Views: 3174
Reputation: 990
Your Route:
Route::post('uploadFile', 'YourController@uploadFile');
Your HTML blade:
<form action="{{ url('uploadFile') }}" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
Your Controller:
public function uploadFile()
{
//get the file
$file = Input::file('file');
//create a file path
$path = 'uploads/';
//get the file name
$file_name = $file->getClientOriginalName();
//save the file to your path
$file->move($path , $file_name); //( the file path , Name of the file)
//save that to your database
$new_file = new Uploads(); //your database model
$new_file->file_path = $path . $file_name;
$new_file->save();
//return something (sorry, this is a habbit of mine)
return 'something';
}
Useful resources (these links may expire, so they are only here for reference):
Laravel Requests (like Inputs and the such)
File Upload Tutorial that helped me when I started
Upvotes: 3