Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Move Files or Folder to One Folder to Another in Laravel

How to Move Files or Folder to One Folder to Another in Laravel ?

Aman Jain, May 27, 2022

Move command is used to shift the folder or files from one location to another without copy so In this article i will show you move files or folder to one folder to another in Laravel. File System is used to perform the file system based operations like move files and folder. For this purpose laravel uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the file system and we can do multiple robust operations using the libraries.

Sometime in our application we want to move the folder or files from one directory to another in this case we can use File::move($source,$destination) or Storage::move($source,$destination) of laravel inbuilt 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 File::move($source,$destination) method to move the entire directory recursively in the folder. move Method accepts two parameters as below

File::move($source, $destination)

or

Storage::move($source, $destination)

Example Usage:

File::move(public("images"),public("movedimages"));

Let’s understand move Folder or files Recursively from one folder to another in Laravel 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 performMoveFiles

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 performMoveFiles(Request $request){
        
            $sourcePath=public_path('images');
            $destinationPath=public_path('movedimages');
         
            if (!File::exists( $storageDestinationPath)) {
                File::move($sourcePath,$storageDestinationPath);
                 
            }
            return response("Folder moved successfully");
         }
  
      }
}

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

Example 2 :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
class FolderController extends Controller
{
    public function performMoveFiles(Request $request){
        
            Storage::move("exist/image.jpg","new/profile.jpg");
            return response("File moved successfully");
         }
  
      }
}

Here we are moving exist/image.jpg to new location with rename new/profile.jpg.

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("/copy-directory",[FolderController::class,"performMoveFiles"]);

Related

Php Laravel Laravel 9 filesfolderlaravelmove

Post navigation

Previous post
Next post

Related Posts

Php How to Create a Custom Artisan Command in LaravelHow to Create a Custom Artisan Command in Laravel

How to Create a Custom Artisan Command in Laravel ?

May 19, 2022May 26, 2022

Laravel artisan provides several inbuilt commands to help the developer to build a robust applications like to create the controller, models, migration etc. and in this article i will show you to create your own custom artisan command in laravel. This custom artisan can be configure according to your needs…

Read More
Php How to Change Date Format in Json Response Laravel

How to Change Date Format in Json Response Laravel ?

July 9, 2022November 17, 2023

In json response we get different data format and to change date format in Json Response Laravel we need to make bit extra efforts to show correct date format. Laravel provides by default two timestamp created_at and updated_at, and there format are according to database format but sometimes we want…

Read More

How to use if else condition in Laravel 8 blade file ?

December 17, 2021January 18, 2022

In this tutorial i will show you to use if else, if and else if in Laravel blade. Laravel use blade engine to render the template and blade have rich features to use conditional statements in file. So here i will show you to use @if, @elseif and @else in…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version