Dynamically create a new folder when uploading file

Dynamically create a new folder when uploading file

 Dynamically create a new folder when uploading the file

Step 1: Update Your Database Credentials

After that update your database credentials in your .env file which is located in your project root.

1. connection databases


Step 2: form upload file

form upload

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

    body {
        color: #999;
        background: #f5f5f5;
        font-family: 'Roboto', sans-serif;
    .form-control, .form-control:focus, .input-group-addon {
        border-color: #e1e1e1;
        border-radius: 0;
    .signup-form {
        width: 50%;
        margin: 0 auto;
        padding: 30px 0;
    .signup-form h2 {
        color: #636363;
        margin: 0 0 15px;
        text-align: center;
    .signup-form .lead {
        font-size: 14px;
        margin-bottom: 30px;
        text-align: center;
    .signup-form form {		
        border-radius: 1px;
        margin-bottom: 15px;
        background: #fff;
        border: 1px solid #f3f3f3;
        box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
        padding: 30px;
    .signup-form .form-group {
        margin-bottom: 20px;
    .signup-form label {
        font-weight: normal;
        font-size: 13px;
    .signup-form .form-control {
        min-height: 38px;
        box-shadow: none !important;
        border-width: 0 0 1px 0;
    .signup-form .input-group-addon {
        max-width: 42px;
        text-align: center;
        background: none;
        border-bottom: 1px solid #e1e1e1;
        padding-left: 5px;
    .signup-form .btn, .signup-form .btn:active {        
        font-size: 16px;
        font-weight: bold;
        background: #19aa8d !important;
        border-radius: 3px;
        border: none;
        min-width: 140px;
    .signup-form .btn:hover, .signup-form .btn:focus {
        background: #179b81 !important;
    .signup-form a {
        color: #19aa8d;
        text-decoration: none;
    .signup-form a:hover {
        text-decoration: underline;
    .signup-form .fa {
        font-size: 21px;
        position: relative;
        top: 8px;
<div class="signup-form">	
    <form action="{{ route('form/save') }}" method="post" enctype="multipart/form-data">
		<h2>Create Form File Upload</h2>
        <div class="form-group">
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-cloud-upload"></i></span>
				<input type="file" class="form-control" name="fileupload[]" required="required" multiple>
		<div class="form-group">
            <button type="submit" class="btn btn-primary btn-block btn-lg">Save</button>

Step 3: Controller

controller upload


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Carbon\Carbon;
use App\Models\formBasic;

class FormController extends Controller
    //view page
    public function index()
        return view('form');
    // save record
    public function saveRecord(Request $request)
        // upload file
        $dt = Carbon::now();
        $folder_name= 'upload';
        $date_time = $dt->toDayDateTimeString();
        \Storage::disk('local')->makeDirectory($folder_name, 0775, true); //creates directory
        if ($request->hasFile('fileupload')) {
            foreach ($request->fileupload as $photo) {
                $destinationPath = $folder_name.'/';
                $file_name = $photo->getClientOriginalName(); //Get file original name                   
                $upload_tbl = [
                    'path'=> $destinationPath.$file_name,

        return redirect()->back();

Step 4: Route



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormController;

| 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('/', function () {
    return redirect('form/new');

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

Step 5: databases migrate

table migrate


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUplaodTblsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('uplaod_tbl', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Step 6: Add in Home Page

php artisan migrate

Step 7: Run

php artisan serve


Post a Comment