ItSolutionStuff.com

Laravel Copy File from One Folder to Another Example

By Hardik Savani • April 16, 2024
Laravel

This example is focused on how to copy file from one folder to another in laravel. we will help you to give example of copy files from one folder to another in laravel. you can understand a concept of laravel copy file from one disk to another. you will learn copy file in laravel.

If you require to copy 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 easily copy file in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 in this post solution.

Example 1: File Facade

Syntax:

File::copy(from_path, to_path);

Example:

In this example, i have one folder "exist" with test.png image in public folder. we will copy this file to new folder "copy" with rename file test_copy.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 copyImage(Request $request)

{

File::copy(public_path('exist/test.png'), public_path('copy/test_copy.png'));

dd('Copy File dont.');

}

}

Example 2: Storage Facade

Syntax:

Storage::copy(from_path, to_path);

Example:

In this example, i have one folder "exist" with test.png image in storage. we will copy this file to new folder "copy" with rename file test_copy.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 copyImage(Request $request)

{

Storage::copy('exist/test.png', 'copy/test_copy.png');

dd('Copy File dont.');

}

}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Move File from One Folder to Another Example

Read Now →

How to Display Data from Json File in Angular?

Read Now →

How to Get File Extension from Path in Laravel?

Read Now →

How to Create Blade File in Laravel using CMD?

Read Now →

Laravel 5 amazon s3 file upload tutorial - Part 1

Read Now →

How to Clear Log File using Command in Laravel?

Read Now →

How to Get File Size from Storage in Laravel?

Read Now →

How to Create Zip File in Laravel?

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →