Laravel File Upload | Laravel Image upload
In this article we going to see how to upload any file ( image,text,mp3 etc ) into our laravel database .In this example we will insert a image file into to the database. without wasting any time lets see a laravel file uploading example .
Steps needed for laravel file upload :
- Create a laravel project
- Create view page / Basic html page
- Crete database
- Create a Model
- Link your database to your project
- Create controller
- File upload codes on controller
Creating a Laravel Project :
Firstly create your laravel project on your desired directory to perform laravel file upload operation . Use this following command to create a project .
- composer create-project laravel/laravel project_name
Create view page / Basic html page :
After creating the project successfully we need to setup our basic html page for image uploading . Open your project on any of your text editor and follow the steps.
you will get your welcome page on the following directory .Then write your file uploading
code.
- project/resources/views/welcome.blade.php
welcome.blade.php ( source code ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> <!-- Styles --> </head> <body> <form action="{{ route('store') }}" method="post" enctype="multipart/form-data"> {{ csrf_field() }} <input type="file" name="image"> <input type="submit" name="submit"> </form> </body> </html> |
Creating Database and table :
In this example i have created a database named "img upload" and create a table named "image" with two colomns such as "id " and "image" .The next step is to create a model ;
Creating Model :
To create a model use the following command : -
- php artisan make:model model_name
- Model file location ( project/app/modelname.php )
After creating model you need to do an optional thing . whenever we insert data into the database laravel automatically creates two timestamp columns inside our table . if you don't want those timestamp colomns then follow these steps otherwise skip these steps.
Goto your model file and use this following code .
image.php ( model ) source code :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class image extends Model
{
protected $table='image';
public $timestamps=false;
}
Linking database to project :
Go to your " .env " file present inside your project ( project/.env ) and provide your database name , username and password as given below
create controller :
Create a controller as shown below
Write the following code on your controller to upload your file on database
imageController.php ( sourcecode ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\image;
class uploadController extends Controller
{
public function store(Request $request)
{
$image=new image;
if($request->hasfile('image'))
{
$file=$request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$file->move('public/upload/userimg/',$filename);
$image->image=$filename;
}
else
{
return $request;
$image->image='';
}
$image->save();
return view('welcome');
}
}
|
Check for working or not :
After successfull completion of the above steps you can successfully upload your files.
Thank you for reading this article .
Nice one
ReplyDelete