How To Copy File From One Folder To Another In Laravel

Hello artisan, in this example tutorial i will show how to create copy file from one folder to another folder in Laravel 8. I will show you step by step guide to create this copy file system in Laravel.

We can create this system using two facades like File facades and another is Storage facades. I will show you two example with File facades and Storage facades. Sometimes we need to copy file from one folder to another folder in Laravel.

 

Example 1: Using File Facade

In this step i will show you copy file from one folder to another folder using File facades. See the systex like below:

 

Syntex
File::copy(from, to);

 

Example code:

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/copy.png'));
   
        dd('success!');
    }
}

 

Example 2: Using Storage Facade

In this step i will show you copy file from one folder to another folder using Storage facades. See the systex like below:

 

Syntex
Storage::copy(from, to);

 

Example code:

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/copy.png');
   
        dd('success.');
    }
}

 

Read also: Best Way to Print Nested Object or Array in Laravel

 

Hope it can help you.

 

#laravel #laravel-8x