Tuesday, May 8, 2012

Copy entire directory in PHP



In PHP, we can copy a file using function copy, rename a file using rename function.
 How about copy entire directory?
Below is the function to copy entire directory.
<?
//string $source      the source directory
//string $dest         the destination directory
function copys($source,$dest)
{
   if (!is_dir($source)) {
      return false;
   }
   if (!is_dir($dest))   {
      mkdir($dest);
   }
 
   $h=@dir($source);
   while (@($entry=$h->read()) !== false) {
      if (($entry == '.') || ($entry == '..')) {
         continue;
      }

      if (is_dir("$source/$entry") && $dest!=="$source/$entry") {
         copys("$source/$entry", "$dest/$entry");
      } else {
         @copy("$source/$entry", "$dest/$entry");
      }
   }
   $h->close();
   return true;
}

?>

No comments:

Post a Comment