0

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); 
       } 
} 
Nishant
  • 2,975
  • 23
  • 38
jordan dap
  • 368
  • 3
  • 19
  • 2
    Possible duplicate of [Copy entire contents of a directory to another using php](http://stackoverflow.com/questions/2050859/copy-entire-contents-of-a-directory-to-another-using-php) – Nirnae Oct 29 '15 at 13:36
  • you can copy the files from one folder to another folder with the above code. copyfolder('source folder', 'destination folder'). Hope u understand the code – Nouphal.M Oct 29 '15 at 13:39
  • Look at this answer: Copy entire contents of a directory to another using php I think this is what you are looking for, enjoy. – fico7489 Oct 29 '15 at 13:51
  • ok working perfectly ,thanks every one – jordan dap Oct 29 '15 at 13:57

2 Answers2

0

Why not using the system function:

system("/bin/cp $source $destination");
mlewis54
  • 2,372
  • 6
  • 36
  • 58
0

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";
}
?>
mstruebing
  • 1,674
  • 1
  • 16
  • 29
  • You say _should_. Is that a guess? The PHP compiler source code says _argument to copy() function cannot be a directory_. – Sam Hobbs Mar 03 '19 at 21:59