Upload file multiple and display for download Laravel 8

Upload file multiple and display for download Laravel 8

Laravel File Upload with DB Storage

Step 1: Configure Database

Update your .env file in your Laravel root directory:

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

Then run:

php artisan config:cache

Step 2: Create File Upload Form (Blade View)

File: resources/views/form.blade.php

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Form Upload</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> </head> <body> <div class="container mt-5"> <h2 class="text-center">Create Form File Upload</h2> <form action="{{ route('form/save') }}" method="post" enctype="multipart/form-data"> @csrf <div class="form-group"> <label>Select Files</label> <input type="file" class="form-control" name="fileupload[]" multiple required> </div> <button type="submit" class="btn btn-success">Save</button> </form> <hr> <table class="table mt-4 table-bordered"> <thead> <tr> <th>No</th> <th>Path</th> <th>File Name</th> <th>Date Time</th> </tr> </thead> <tbody> @foreach ($data as $key => $item) <tr> <td>{{ ++$key }}</td> <td>{{ $item->path }}</td> <td><a href="#">{{ $item->file_name }}</a></td> <td>{{ $item->datetime }}</td> </tr> @endforeach </tbody> </table> </div> </body> </html>

Step 3: Create Controller

File: app/Http/Controllers/FormController.php

namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Carbon\Carbon; class FormController extends Controller { public function index() { $data = DB::table('uplaod_tbl')->get(); return view('form', compact('data')); } public function saveRecord(Request $request) { $folder = 'upload'; $datetime = Carbon::now()->toDateTimeString(); if ($request->hasFile('fileupload')) { foreach ($request->fileupload as $file) { $fileName = $file->getClientOriginalName(); $filePath = $folder . '/' . $fileName; // Save to local disk Storage::disk('local')->put($filePath, file_get_contents($file->getRealPath())); // Save to DB DB::table('uplaod_tbl')->insert([ 'file_name' => $fileName, 'path' => $filePath, 'datetime' => $datetime, 'created_at' => now(), 'updated_at' => now(), ]); } } return redirect()->back(); } }

Step 4: Add Routes

File: routes/web.php

use App\Http\Controllers\FormController; Route::get('/', function () { return redirect('form/new'); }); Route::get('form/new', [FormController::class, 'index'])->name('form/new'); Route::post('form/save', [FormController::class, 'saveRecord'])->name('form/save');

Step 5: Create Migration

Run this command to create the table migration:

php artisan make:migration create_uplaod_tbl_table

Then, update the generated file:

Schema::create('uplaod_tbl', function (Blueprint $table) { $table->id(); $table->string('file_name')->nullable(); $table->string('path')->nullable(); $table->string('datetime')->nullable(); $table->timestamps(); });

Then run:

php artisan migrate

Step 6: Run the Project

php artisan serve

Visit: http://127.0.0.1:8000/form/new

Summary

StepDescription
.envDatabase config
ViewForm and file listing
ControllerHandles file upload and DB insert
RouteRoutes for form and save
MigrationCreate uplaod_tbl table
ServeLaunch app with php artisan serve

Let me know if you'd like to add:

  • File preview/download

  • Delete functionality

  • Storage in public folder instead of local

  • Validation (file types, size)

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

1 Comments

CAN FEEDBACK
  1. Anonymous
    Anonymous
    ErrorException
    file_get_contents(C:\Apache24\htdocs\multiple-file-upload\public): failed to open stream: Permission denied

    How to fix this ?
    error in FormController
    Storage::disk('local')->put($folder_name.'/'.$file_name,file_get_contents($photo->getRealPath()));
close