How copy entire directory from one folder to another using laravel 5
Asked Answered
S

6

5

May be it's so easy but I couldn't find any proper answer in google or here!!

I did use

\File::copyDirectory('public/' . $token . '/cover/', $folderName . '/cover/');

but there's no different!!

This is where I found above solution.

Do you know how can I copy entire directory from one folder to another using laravel 5.4?

Thanks in Advance

Sheep answered 21/6, 2017 at 0:59 Comment(2)
Try passing an absolute path as opposed to a relative path. perhaps your base directory is not what it seems to be when you execute the code. (I'm saying to prepend a / in front of public.Malm
@Sheep can you please aprove my awnser?Ulrika
U
16

More than one year later :)

Use path helpers functions https://laravel.com/docs/5.6/helpers

\File::copyDirectory( public_path . 'to/the/app', resource_path('to/the/app'));
Ulrika answered 24/7, 2018 at 12:56 Comment(1)
That function is not present on the linked helpers page in 5.6? Even weirder searching "copyDirectory" in laravels entire documentation and there are no results in 8 and 9 either. Does anyone know why the file facade and lots of functions seems to be missing from the documentation entirely? It's very confusing when they are very much present in the source code still.Enfield
E
8

We can do it in laravel 5.8 using file facade as followings:

use Illuminate\Support\Facades\File;  

File::copyDirectory(__DIR__.'/form-directory', resource_path('to-directory'));

Hope, it'll be helpful.

Embolus answered 8/5, 2019 at 15:3 Comment(0)
K
1

Just do this:

   /* Move test images from public folder to storage folder */
    $source = "source/path";
    $destination = "public/files";

    File::copyDirectory(base_path($source), base_path($destination));
Keepsake answered 19/1, 2020 at 18:36 Comment(0)
O
0

I used Storage facades and worked well

config/filesystems.php

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => public_path(),
    ],
     ...
]

Copy all files from public/seed_images/ to public/images/

app/Http/Controllers/HomeController.php

use Illuminate\Support\Facades\Storage;
$fromFiles = Storage::allFiles('seed_images');
for ($i=0; $i < count($fromFiles) ; $i++) {
    $toFile = str_replace("seed_images","images",$fromFiles[$i]);
    Storage::copy( $fromFiles[$i], $toFile );
}
Ogden answered 6/6, 2021 at 1:8 Comment(0)
A
0
/**
 * If the destination directory does not actually exist, we will go 
 * ahead and create it recursively, which just gets the destination 
 * prepared to copy the files over. Once we make the directory we'll 
 * proceed the copying.
 *
 * function replaces files with same names
 * 
 * @var bool
 */
$result = File::copyDirectory(
    base_path() . '/storage/source',
    base_path() . '/storage/destination'
);
Automat answered 10/8, 2023 at 5:50 Comment(0)
G
-3

Try using the mix.copy() method.

mix.copy('public/' . $token . '/cover/', $folderName . '/cover/');

Read more at: https://laravel.com/docs/5.4/mix#copying-files-and-directories

Gravelly answered 21/6, 2017 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.