Copy Folder Contain File menggunakan PHP pada Linux

berikut ini contoh function php untuk melakukan copy folder yang disalam forder tersebut ada file dan folder juga. jadi semua isi folder akan dicopy.


 <?php

function copy_folder($src, $dst) { 

   

    // open the source directory

    $dir = opendir($src); 

   

    // Make the destination directory if not exist

    @mkdir($dst); 

   

    // Loop through the files in source directory

    foreach (scandir($src) as $file) { 

   

        if (( $file != '.' ) && ( $file != '..' )) { 

            if ( is_dir($src . '/' . $file) ) 

            { 

   

                // Recursively calling custom copy function

                // for sub directory 

                copy_folder($src . '/' . $file, $dst . '/' . $file); 

   

            } 

            else { 

                copy($src . '/' . $file, $dst . '/' . $file); 

            } 

        } 

    } 

   

    closedir($dir);


// Make sure source folder have sufficient permission to read files

$src = "/www/wwwroot/domain.id/newfolder";

$dst = "/www/wwwroot/domain.id/newfolder-2";

  

copy_folder($src, $dst);


Selamat mencoba!

referensi: https://onlinewebtutorblog.com/copy-entire-contents-of-a-directory-to-another-directory-in-php/