DEV Community

Cover image for How To Move File from One Folder to Another In Laravel
Saddam Hossain
Saddam Hossain

Posted on

1

How To Move File from One Folder to Another In Laravel

In This Tutorial I will Show you How To Move File from One Folder to Another In Laravel Application.

This article will provide some of the most important example how to move file from one folder to another in laravel. this example will help you move files from one folder to another in laravel. I’m going to show you about laravel cut file from one disk to another.

If you require to move file from one folder to another in laravel application then i will help you how to do it in laravel. laravel provide File and Storage facade and their method to work with file system. i will give you both way example with syntax so you can use it. You Can Learn How to generate fake data using factory tinker in laravel 11 Example

You can easily move file in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 in this post solution.

How To Move File from One Folder to Another In Laravel

Example 1: File Facade
Syntax:

File::move(from_path, to_path);
Enter fullscreen mode Exit fullscreen mode

Example:

In this example, i have one folder “exist” with test.png image in public folder. we will move this file to new folder “move” with rename file test_move.png. so let’s see bellow code.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File; 

class DemoController extends Controller
{
    /**
     * Write code on Construct
     *
     * @return \Illuminate\Http\Response
     */  

    public function moveImage(Request $request)
    {

        File::move(public_path('exist/test.png'), public_path('move/test_move.png'));
        dd('Copy File dont.');

    }

}
Enter fullscreen mode Exit fullscreen mode

Example 2: Storage Facade

Syntax:

Storage::move(from_path, to_path);
Enter fullscreen mode Exit fullscreen mode

Example:

In this example, i have one folder “exist” with test.png image in storage. we will move this file to new folder “move” with rename file test_move.png. so let’s see bellow code.

<?php  
namespace App\Http\Controllers; 

use Illuminate\Http\Request;
use Storage;  
class DemoController extends Controller
{    
/**     
* Write code on Construct     
*     
* @return \Illuminate\Http\Response     
*/      
public function moveImage(Request $request)    
{        
Storage::move('exist/test.png', 'move/test_move.png');           
dd('Copy File dont.');    
}}
Enter fullscreen mode Exit fullscreen mode

I Think it’s Help You.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay