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 Understanding laravel request

Understanding Laravel request with example

November 17, 2021November 22, 2021

Laravel provides a own class to handle the request which is Illuminate\Http\Request. Request is used to retrieve the user input(GET and POST), cookies and files that was submitted during the request. Get the request in controller Current Request can be get retrieved in controller by injecting the dependency in controller…

Read More
Php How to Upload image with preview in Laravel 9 with example

How to Upload image with preview in Laravel 9 with example ?

May 14, 2022May 14, 2022

Upload image with preview in laravel or for any website can be basic requirement like set up a profile picture to providing the documents. Laravel 9 provides robust functionality to upload and process the image with security. Laravel simply use Request file method to access the uploaded file and then…

Read More
Php Unique Validation in laravel 8

Unique validation on Update in Laravel with example

June 19, 2022November 17, 2023

In this article we will learn to handle the unique validation on update in laravel.As we know Laravel 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…

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