Skip to content

Instantly share code, notes, and snippets.

@vovadocent
Created December 22, 2016 10:59
Show Gist options
  • Save vovadocent/acf50326280d26113b6d20e3137145ae to your computer and use it in GitHub Desktop.
Save vovadocent/acf50326280d26113b6d20e3137145ae to your computer and use it in GitHub Desktop.
Copy and Remove Noempty Dir Functions
<?php
function copy_dir($source, $target) {
if (is_dir($source)) {
mkdir($target, 0777);
$d = dir($source);
while (FALSE !== ( $entry = $d->read())) {
if ($entry == '.' || $entry == '..')
continue;
$Entry = $source . '/' . $entry;
if (is_dir($Entry)) {
copy_dir($Entry, $target . '/' . $entry);
continue;
}
copy($Entry, $target . '/' . $entry);
}
$d->close();
chmod($target, 0755);
} else {
copy($source, $target);
chmod($target, 0644);
}
}
function removeDirectory($path) {
// The preg_replace is necessary in order to traverse certain types of folder paths (such as /dir/[[dir2]]/dir3.abc#/)
// The {,.}* with GLOB_BRACE is necessary to pull all hidden files (have to remove or get "Directory not empty" errors)
$files = glob(preg_replace('/(\*|\?|\[)/', '[$1]', $path).'/{,.}*', GLOB_BRACE);
foreach ($files as $file) {
if ($file == $path.'/.' || $file == $path.'/..') { continue; } // skip special dir entries
is_dir($file) ? removeDirectory($file) : unlink($file);
}
@rmdir($path);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment