A small code snippet, which is very common yet useful. Let’s write a function which will copy a folder/directory and its contents (including subfolders, files) from one place to other.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public static function copyFolder($source,$dest) { // recursive function if(is_dir( $source)) { // if its directory @mkdir($dest); //make it $d = dir($source); // create directory object while(FALSE!==($entry=$d->read())) { // read each file of directory if($entry=='.' || $entry == '..') { //exclude root and parent continue; } $newentry = $source . '/' . $entry; // get sub if(is_dir($newentry)) { // if sub is directory self::copyFolder($newentry,$dest.'/'.$entry); // then go recursive continue; } copy($newentry, $dest.'/'.$entry ); //else copy file } $d->close(); } else { copy($source,$dest); // copy file } } |
I hope this code will be a helpful contribution. Please do let me know.





