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 Enum Field in Laravel Migration

How to Create Enum Field in Laravel Migration ?

June 30, 2022August 17, 2022

In laravel while creating table schema for some columns we want to add column type enum field so in this article we will create Enum Field in Laravel Migration. We can easily add enum column as we add other varchar integer etc columns in laravel. Enum is useful when we…

Read More
Php Unique Validation in laravel 8

Unique validation in Laravel with example

November 21, 2021June 17, 2022

Laravel also support validation for database operations. Laravel unique validation validates the table column uniqueness using unique validation. In this tutorial we will learn about the unique validation to table columns and also while updating the value too. you can use this to check the uniqueness of email, username ,…

Read More
Php How to ShareSend Message to WhatsApp in Laravel PHP

How to Share/Send Message to WhatsApp in Laravel PHP ?

March 16, 2022March 16, 2022

Sharing or sending message to whatsapp from a website using a link is simple as possible. In this article we will not use any package or third party tool to share the url or message to whatsapp. Whatsapp itself provide to share or send message using the url so 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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version