Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Check folder exist and Create a Nested Folder in Laravel

How to Check folder exist and Create Nested Folder in Laravel ?

Aman Jain, May 24, 2022May 26, 2022

This article will guide you to Check folder exist and Create Nested Folder in Laravel. To perform the file system based operations like Check folder exist and Create a Nested Folder. we use File class in laravel. Laravel provides inbuilt library to access the file system and we can do multiple robust operations using the libraries. In this tutorial i will show you to make a directory in laravel and also i will show you to create nested folder in using the library. If we perform same operations using the core php functions than it can be a hectic or lengthy code to implement but using the packages or library it will be easier to implement.

In this article I will use Illuminate\Filesystem\Filesystem class and facade File::makeDirectory() method to create the new directory in the folder. makeDirectory Method accepts four parameters as below

File::makeDirectory($path, $mode, $recursive, $force)

Example Usage:

File::makeDirectory(public("images/user/1"), 0777, true);

Here I called makeDirectory with 3 parameters first parameter tells to create the nested folder as images > user > 1, second parameter for permission of the folder and third for creating the recursive folders so in nutshell

$path : Path of directory to create.

$mode : Permission of folders

$recursive : Create directory recursively

$force : To create directory forcefully

Let’s understand it with example step by step

Step 1: Create a fresh laravel project

Open a terminal window and type below command to create a new project

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

You can also read this to start with new project

Step 2 : Create controller

Let’s create a controller and add method checkAndCreateDirectory

php artisan make:controller FolderController

and add the below code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
class FolderController extends Controller
{
    public function checkAndCreateDirectory(Request $request){
        
        if($request->folder_name!=''){
            $storageDestinationPath=storage_path('app/'.$request->folder_name);
         
            if (!File::exists( $storageDestinationPath)) {
                File::makeDirectory($storageDestinationPath, 0755, true);
            }
            return response("Folder created successfully");
         }
  
      }
}

In above code First of all we are checking the directory is exist or not then if not available then we are creating the directory at the location.

Step 3: Create two routes in routes/web.php

Create a route to create the directory

routes/web.php

<?php

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

Route::get("/create-directory",[FolderController::class,"checkAndCreateDirectory"]);
Check folder exist and Create a Nested Folder in Laravel
Check folder exist and Create a Nested Folder in Laravel
Check folder exist and Create a Nested Folder in Laravel
Check folder exist and Create a Nested Folder in Laravel

Related

Php Laravel Laravel 9 existlaravelmkdir

Post navigation

Previous post
Next post

Related Posts

Uncategorized Custom middleware in laravel

How to create custom middleware Laravel 8

October 15, 2021November 6, 2023

In this tutorial we will going to learn custom middleware Laravel 8. Middleware are used to create a layer between request and response of the http request. it filters or create a logic before the request serve to the controller and also filter or modify the response. we can also…

Read More
Laravel Call Controller Method from Another Controller in Laravel

How to Call Controller Method from Another Controller in Laravel ?

December 2, 2023March 16, 2024

In this article we will learn Call Controller Method from Another Controller in Laravel. Sometimes we need to access the controller method from another controller to save the same code on multiple locations. Best way to achieve it is using create a separate service class or trait in PHP so…

Read More
Uncategorized Laravel ajax form with example

Submit form using jQuery in Laravel

November 18, 2021November 18, 2021

jQuery is most popular library to handle the client side events like form submit, click, change etc. In this article we will learn to submit the form using jQuery Ajax method. Ajax is mostly used when we do not want to reload the page and real time interaction. In this…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version