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

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 Illuminate\Database\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. public $fillable = ['name','details'];
  6. }
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');

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

app/Http/Controllers/FileController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Product;
  6. class FileController extends Controller {
  7. public function index(Request $request){
  8. $products = Product::orderBy('id','DESC')->paginate(5);
  9. return view('files.index',compact('products'))
  10. ->with('i', ($request->input('page', 1) - 1) * 5);
  11. }
  12. public function create(){
  13. return view('files.create');
  14. }
  15.     public function store(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 = new Product($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.          return redirect()->route('upload-files.index')
  34. ->with('success','You have successfully uploaded your files');
  35. }
  36.     
  37. }
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. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>List of Product Files</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-success" href="{{ route('upload-files.create') }}"> Upload New File</a>
  10. </div>
  11. </div>
  12. </div>
  13. <table class="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. <a href='{{ asset("images/$product->product_image") }}'>{{ $product->product_image }}</a>
  27. </td>
  28. </tr>
  29. @endforeach
  30. </table>
  31. {!! $products->render() !!}
  32. @endsection

Now at last create a form to upload file.

resources/views/files/create.blade.php

  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>Upload Files</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="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. <div class="row">
  15. <div class="col-xs-12 col-sm-12 col-md-6">
  16. <div class="form-group">
  17. <strong>Name:</strong>
  18. {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
  19. </div>
  20. </div>
  21. <div class="col-xs-12 col-sm-12 col-md-6">
  22. <div class="form-group">
  23. <strong>Upload File:</strong>
  24. {!! Form::file('product_image', array('class' => 'form-control')) !!}
  25. </div>
  26. </div>
  27. <div class="col-xs-12 col-sm-12 col-md-12">
  28. <div class="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. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  34. <button type="submit" class="btn btn-primary">Submit</button>
  35. </div>
  36. </div>
  37. {!! Form::close() !!}
  38. @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

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
escort bayan escort bayan escort bayan escort bayan escort bayan escort bayan escort bayan sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort adapazarı escort adapazarı escort adapazarı escort adapazarı escort adapazarı escort adapazarı escort adapazarı escort serdivan escort serdivan escort serdivan escort serdivan escort serdivan escort serdivan escort serdivan escort sakarya escort bayan sakarya escort bayan sakarya escort bayan sakarya escort bayan sakarya escort bayan sakarya escort bayan sakarya escort bayan escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya serdivan escort sapanca escort karasu escort hendek escort izmir escort