How to Upload File or Image With Validation and List File From Database in Laravel 5.2

How to Upload File or Image With Validation and List File From Database in Laravel 5.2

In this post we will give you information about How to Upload File or Image With Validation and List File From Database in Laravel 5.2. Hear we will give you detail about How to Upload File or Image With Validation and List File From Database in Laravel 5.2And how to use it also give you demo for it if it is necessary.

How to Upload File or Image With Validation and List File From Database in Laravel 5.2

As you know, Laravel provides awesome feature and It’s very easy PHP Framework for developer to work with Laravel.

Working with upload file for image with validation in Laravel 5.2 is very easier and easy to validate files and their type in Laravel 5.2.

Here, you will find the complete code in Laravel 5.2 to upload files or images and store file name in database and displaye files or images to download . There are so many helper class available in Laravel and Form::file helper class is one of them.

Step1: Create Product Table And Model

First, you will have to create a table where you store file name, file path, file extention, file size and all. So We are going to create a product model according to table.

  1. namespace App;
  2. use IlluminateDatabaseEloquentModel;
  3. class Product extends Model
  4. {
  5. public $fillable=['name','details'];
  6. }
namespace App;use IlluminateDatabaseEloquentModel;class Product extends Model{    public $fillable = ['name','details'];}

Step2: Add Route and Controller

Add below line of code in your routes.php in following path app/Http/routes.php

app/Http/routes.php

  1. Route::resource('upload-files','FileController');
Route::resource('upload-files','FileController');

Now you will create FileController.php in following path app/Http/Controllers

app/Http/Controllers/FileController.php

  1. <?php
  2. namespace AppHttpControllers;
  3. use IlluminateHttpRequest;
  4. use AppHttpControllersController;
  5. use AppProduct;
  6. class FileController extends Controller {
  7. public functionindex(Request $request){
  8. $products= Product::orderBy('id','DESC')->paginate(5);
  9. returnview('files.index',compact('products'))
  10. ->with('i',($request->input('page',1)-1)*5);
  11. }
  12. public functioncreate(){
  13. returnview('files.create');
  14. }
  15.     public functionstore(Request $request){
  16. $this->validate($request,[
  17. 'name'=>'required',
  18. 'details'=>'required',
  19. 'product_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  20. ]);
  21. $product=newProduct($request->input());
  22.     
  23.         if($file=$request->hasFile('product_image')){
  24.             
  25.             $file=$request->file('product_image');
  26.             
  27.             $fileName=$file->getClientOriginalName();
  28.         $destinationPath=public_path().'/images/';
  29.             $file->move($destinationPath,$fileName);
  30.             $product->product_image =$fileName;
  31.         }
  32.         $product->save();
  33.          returnredirect()->route('upload-files.index')
  34. ->with('success','You have successfully uploaded your files');
  35. }
  36.     
  37. }
<?phpnamespace AppHttpControllers;use IlluminateHttpRequest;use AppHttpControllersController;use AppProduct;class FileController extends Controller {    public function index(Request $request){        $products = Product::orderBy('id','DESC')->paginate(5);        return view('files.index',compact('products'))            ->with('i', ($request->input('page', 1) - 1) * 5);    }    public function create(){        return view('files.create');    }	public function store(Request $request) {                $this->validate($request, [            'name' => 'required',            'details' => 'required',        ]);        $product = new Product($request->input()) ; 	 		if($file = $request->hasFile('product_image')) {						$file = $request->file('product_image') ;						$fileName = $file->getClientOriginalName() ;    		$destinationPath = public_path().'/images/' ;			$file->move($destinationPath,$fileName);			$product->product_image = $fileName ;		}		$product->save() ;		 return redirect()->route('upload-files.index')                        ->with('success','You have successfully uploaded your files');    }	}

Step3: View File

Create a directory ‘files’ in following path resources/views/ and then create index.blade.php in files directory to list down all product and their files.

resources/views/files/index.blade.php

  1. @extends('layouts.default')
  2. @section('content')
  3. <divclass="row">
  4. <divclass="col-lg-12 margin-tb">
  5. <divclass="pull-left">
  6. <h2>List of Product Files</h2>
  7. </div>
  8. <divclass="pull-right">
  9. <aclass="btn btn-success"href="{{ route('upload-files.create') }}"> Upload New File</a>
  10. </div>
  11. </div>
  12. </div>
  13. <tableclass="table table-bordered">
  14. <tr>
  15. <th>No</th>
  16. <th>Name</th>
  17. <th>Details</th>
  18. <th>Your File</th>
  19. </tr>
  20. @foreach ($products as $product)
  21. <tr>
  22. <td>{{ ++$i }}</td>
  23. <td>{{ $product->name }}</td>
  24. <td>{{ $product->details }}</td>
  25. <td>
  26. <ahref='{{asset("images/$product->product_image")}}'>{{ $product->product_image }}</a>
  27. </td>
  28. </tr>
  29. @endforeach
  30. </table>
  31. {!! $products->render() !!}
  32. @endsection
@extends('layouts.default')@section('content')    <div >        <div >            <div >                <h2>List of Product Files</h2>            </div>            <div >                <a  href="{{ route('upload-files.create') }}"> Upload New File</a>            </div>        </div>    </div>    <table >        <tr>            <th>No</th>            <th>Name</th>            <th>Details</th>            <th>Your File</th>        </tr>    @foreach ($products as $product)    <tr>        <td>{{ ++$i }}</td>        <td>{{ $product->name }}</td>        <td>{{ $product->details }}</td>        <td>        <a href='{{ asset("images/$product->product_image") }}'>{{ $product->product_image }}</a>        </td>    </tr>    @endforeach    </table>    {!! $products->render() !!}@endsection

Now at last create a form to upload file.

resources/views/files/create.blade.php

  1. @extends('layouts.default')
  2. @section('content')
  3. <divclass="row">
  4. <divclass="col-lg-12 margin-tb">
  5. <divclass="pull-left">
  6. <h2>Upload Files</h2>
  7. </div>
  8. <divclass="pull-right">
  9. <aclass="btn btn-primary"href="{{ route('upload-files.index') }}"> Back</a>
  10. </div>
  11. </div>
  12. </div>
  13. {!! Form::open(array('route' => 'upload-files.store','method'=>'POST','files'=>true)) !!}
  14. <divclass="row">
  15. <divclass="col-xs-12 col-sm-12 col-md-6">
  16. <divclass="form-group">
  17. <strong>Name:</strong>
  18. {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
  19. </div>
  20. </div>
  21. <divclass="col-xs-12 col-sm-12 col-md-6">
  22. <divclass="form-group">
  23. <strong>Upload File:</strong>
  24. {!! Form::file('product_image', array('class' => 'form-control')) !!}
  25. </div>
  26. </div>
  27. <divclass="col-xs-12 col-sm-12 col-md-12">
  28. <divclass="form-group">
  29. <strong>Details:</strong>
  30. {!! Form::textarea('details', null, array('placeholder' => 'Details','class' => 'form-control','style'=>'height:100px')) !!}
  31. </div>
  32. </div>
  33. <divclass="col-xs-12 col-sm-12 col-md-12 text-center">
  34. <buttontype="submit"class="btn btn-primary">Submit</button>
  35. </div>
  36. </div>
  37. {!! Form::close() !!}
  38. @endsection
@extends('layouts.default')@section('content')    <div >        <div >            <div >                <h2>Upload Files</h2>            </div>            <div >                <a  href="{{ route('upload-files.index') }}"> Back</a>            </div>        </div>    </div>    {!! Form::open(array('route' => 'upload-files.store','method'=>'POST','files'=>true)) !!}      <div >        <div >            <div >                <strong>Name:</strong>                {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}            </div>        </div>        <div >            <div >                <strong>Upload File:</strong>                {!! Form::file('product_image', array('class' => 'form-control')) !!}            </div>        </div>        <div >            <div >                <strong>Details:</strong>                {!! Form::textarea('details', null, array('placeholder' => 'Details','class' => 'form-control','style'=>'height:100px')) !!}            </div>        </div>        <div >                <button type="submit" >Submit</button>        </div>    </div>    {!! Form::close() !!}@endsection

Now you can try with following code in your application to upload file with validation and display file from database in Laravel 5.2

Show Demo

Hope this code and post will helped you for implement How to Upload File or Image With Validation and List File From Database in Laravel 5.2. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US