Hi Dev,
In this Article i will explained to the Laravel Validation file Example in your laravel project.engendering a feature to upload a file is a common task for any web developer millions of photos, videos and documents daily updated to various websites.Laravel Validation file Example is so easy to use.so you can just follow my step by step and learn Laravel Validation file Example.
So let's start to the example and follow to the my all step.
Solution$request->validate([ 'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048' ]);Step 1: Create Route
First step to create a route in web.php file and use this code.
Route :routes/web.php<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\FileController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('upload/file', [FileController::class, 'index']); Route::post('upload/file/data', [FileController::class, 'uploadFile'])->name('upload.file');Step 2: Create a FileController Controller
in this second step you can need to the FileController so create a FileController in just following command through.
Path : app/Http/Controllers/FileController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Validator; class FileController extends Controller { /** * The attributes that are mass assignable. * * @var array */ public function index(){ return view('uploadFile'); } /** * The attributes that are mass assignable. * * @var array */ public function uploadFile(Request $request){ $request->validate([ 'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048' ]); $file = new File; if($request->file()) { $name = time().'_'.$request->file->getClientOriginalName(); $filePath = $request->file('file')->storeAs('uploads', $name, 'public'); $file->name = time().'_'.$request->file->getClientOriginalName(); $file->file = '/storage/' . $filePath; $file->save(); return back() ->with('success','File has uploaded to the database.') ->with('file', $name); } } }Step 4: Create a UploadFile Blade File Next you can require to the uploadFile.blade.php so create a uploadFile.blade.php in just following step. View :resources/views/uploadFile.blade.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <title>Laravel Validation File Example</title> <style> .container { max-width: 450px; } dl, ol, ul { margin: 0; padding: 0; list-style: none; } </style> </head> <body class="bg-dark"> <div class="container mt-5"> <div class="card" style="width: 25rem;"> <h5 class="card-title text-center text-white mt-3">Laravel Validation File Example</h5> <div class="card-body"> <form action="{{route('upload.file')}}" method="post" enctype="multipart/form-data"> @csrf @if ($msg = Session::get('success')) <div class="alert alert-success"> <strong>{{ $msg }}</strong> </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div class="custom-file"> <input type="file" name="file" class="custom-file-input" id="chooseFile"> <label class="custom-file-label" for="chooseFile">Choose file</label> </div> <button type="submit" name="submit" class="btn btn-primary btn-block mt-4"> Upload Files </button> </form> </div> </div> </div> </body> </html>
So, finally we are done with our code we can get below output.
php artisan serve
Browser url run : http://localhost:8000/upload/file
It Will Help You...
Post a Comment