Copy Directory Content into Sub Directory using PHP


The code below copies a folder content ( or directory) into a newly created subfolder( or subdirectory).

<?php
$myDir = opendir('/myDir');
mkdir('/myDir/newDir');
while (false !== ($file = readdir($myDir))) {
    if (($file != '.') && ($file != '..')) {
        if (is_dir('/myDir/' . $file)) {
            $this->recurseCopy('/myDir/' . $file, '/myDir/newDir/' . $file);
        } else {
            copy('/myDir/' . $file, '/myDir/newDir/' . $file);
        }
    }
}
closedir($myDir);
?>

Happy coding!!!

More PHP Snippets

Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →