简体   繁体   中英

How to copy folder to another folder

I want to copy a folder containing files to another destination using php. I found a code snippet, but I'm not getting how it works.

Is it possible to copy folder to another folder?

copyfolder('folder', 'folder_copy');
function copyfolder($source, $destination) 
{ 
       $directory = opendir($source); 
       mkdir($destination);
       while(($file = readdir($directory)) != false) 
       { 
         copy($source.'/' .$file, $destination.'/'.$file); 
       } 
} 

为什么不使用系统功能:

system("/bin/cp $source $destination");

Try PHP Copy

This should also work with folders.

bool copy ( string $source , string $dest [, resource $context ] )

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "copy $file schlug fehl...\n";
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM