Skip to content

Instantly share code, notes, and snippets.

@somoza
Created June 27, 2019 22:02
Show Gist options
  • Save somoza/520c9a4cce1451efb04a137c27b96b3d to your computer and use it in GitHub Desktop.
Save somoza/520c9a4cce1451efb04a137c27b96b3d to your computer and use it in GitHub Desktop.
Move entire folder and their content with PHP
// Function to remove folders and files
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..") rrmdir("$dir/$file");
rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}
// Function to Copy folders and files
function rcopy($src, $dst) {
if (file_exists ( $dst ))
rrmdir ( $dst );
if (is_dir ( $src )) {
mkdir ( $dst );
$files = scandir ( $src );
foreach ( $files as $file )
if ($file != "." && $file != "..")
rcopy ( "$src/$file", "$dst/$file" );
} else if (file_exists ( $src ))
copy ( $src, $dst );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment