Image not found

How to upload file in laravel?

Comment (0)

Admin

267


Step 1 : Install Laravel Fresh Project

 

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Create Routes 

 

routes/web.php 

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\FileUploadController;


/*
|--------------------------------------------------------------------------
| 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('file-upload-page', [FileUploadController::class , 'fileUploadPage'])->name('file.upload.page');
Route::post('file-upload', [FileUploadController::class , 'fileUploadPost'])->name('file.upload.post');

 

Step 3: Create FileUploadController 

 

app/Http/Controllers/FileUploadController.php 

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class FileUploadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function fileUploadPage()
    {
        return view('fileUpload');
    }
  
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function fileUploadPost(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:pdf,xlx,csv|max:2048',
        ]);
  
        $fileName = time().'.'.$request->file->extension();  
   
        $request->file->move(public_path('uploads'), $fileName);
   
        return back()
            ->with('success','You have successfully upload file.')
            ->with('file',$fileName);
   
    }
}

Note: You have to create "uploads" Foler in public path.

 

How to input only Bangla text. — An input tool for bangla language

 

Step 4: Create Blade File 

app/Http/Controllers/FileUploadController.php

<!DOCTYPE html>
<html>
<head>
    <title>laravel file upload example by nijwel.xyz</title>
    <link rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css">
</head>
  
<body>
<div class="container">
   
    <div class="panel panel-primary">
      <div class="panel-heading"><h2>laravel file upload example by Nijwel</h2></div>
      <div class="panel-body">
   
        @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
        </div>
        <img src="uploads/{{ Session::get('file') }}">
        @endif
  
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
  
        <form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="row">
                <div class="col-md-6">
                    <input type="file" name="file" class="form-control">
                </div>
                <div class="col-md-6">
                    <button type="submit" class="btn btn-success">Upload</button>
                </div>
            </div>
        </form>
      </div>
    </div>
</div>
</body>
  
</html>

 

Hope it will help.............

Thanks ! 


Others Problem Fix Stroy



Comments (0)

Your Comment