article

Monday, May 23, 2022

Laravel Upload Image Validation

Laravel Upload Image Validation

How to install laravel 9

https://tutorial101.blogspot.com/2022/02/how-to-install-laravel-9.html

Connecting our Database

open .env file root directory.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Database Migration
php artisan migrate

C:\xampp\htdocs\laravel9\blog>php artisan migrate
Migration table created successfully.
check database table
Creating Controller

php artisan make:controller ProductController
C:\xampp\htdocs\laravel\blog>php artisan make:controller ProductController
change it with the following codes:

blog\app\Http\Controllers\ProductController.php
//blog\app\Http\Controllers\ProductController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index(){
        return view('index');
    }
    public function validateImage(Request $request){
        $request->validate(['image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|dimensions:min_width=100,min_height=100,max_width=1000,max_height=1000',]);
        return redirect()->back(); 
    }
}
Create Views
blog\resources\views\index.blade.php
//blog\resources\views\index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel Upload Image Validation</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">  
</head>
<body>
<h3 class="text-success" align="center">Laravel Upload Image Validation </h3><br>
<div class="container">
  <div class="card">
    <div class="card-header">
      Laravel Upload Image Validation
    </div>
    <div class="card-body">
      @if(count($errors) > 0)                    
          <div class="alert alert-danger">{{ $errors->first() }}</div>                   
      @endif

      <form action="{{ url('validateimage') }}" method="post" enctype="multipart/form-data">   
            @csrf      
                <div class="panel-body">                 
                    <div class="form-group">
                        <label class="control-label col-sm-2">Select Image:</label>
                        <div class="col-sm-5">
                          <input type="file"  name="image">
                        </div>
                    </div>
                  <br><br>
                    <button class="btn btn-success" type="submit" style='margin-left:20%'>Upload Image</button>
            </div>
      </form>
    </div>
  </div>
</div>
Creating Routes
blog\routes\web.php
//blog\routes\web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController; //add ProductController

//Route::get('/', function () {
//    return view('welcome');
//});
  
Route::get('/', [ProductController::class, 'index']);
Route::post('/validateimage', [ProductController::class, 'validateimage']);
Run C:\xampp\htdocs\laravel\blog>php artisan serve
Starting Laravel development server: http://127.0.0.1:8000

Related Post