Laravel Copy File from One Folder to Another Example (ok)

https://www.itsolutionstuff.com/post/laravel-copy-file-from-one-folder-to-another-exampleexample.html

C:\xampp\htdocs\reset\app\Http\Controllers\DemoController.php

<?php
namespace App\Http\Controllers;
use File;
use Illuminate\Http\Request;
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.');
  }
  /**
   * Write code on Construct
   *
   * @return \Illuminate\Http\Response
   */
  public function copyImageS(Request $request) {
    Storage::copy('exist/test.png', 'copy/test_copy.png');
    dd('Copy File dont.');
  }
}

C:\xampp\htdocs\reset\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/test', [App\Http\Controllers\DemoController::class, 'copyImage'])->name('test');
Route::get('/test2', [App\Http\Controllers\DemoController::class, 'copyImageS'])->name('test2');

Laravel Copy File from One Folder to Another Example

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 and laravel 9 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.

Read Also: How to Delete File from Public Folder / Storage Folder in Laravel?

<?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...

Last updated