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
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort