title: PHP Copy Directory Function
body: <p>I'm working on a Minecraft Server Dashboard, and one of it's functions is to backup and restore your world (a directory). I already have a function (see below), but, as you can probably see, it's pretty bad code. Anyone know of a better, cleaner function?</p>
  
  <pre><code>function backupOrRestoreWorld($source,$target){
      foreach(glob($target.'*.*')as$v){
          unlink($v);
      }
      if(is_dir($source)){
          @mkdir($target);
          $d=dir($source);
          while(FALSE!==($entry=$d-&gt;read())){
              if($entry=='.'||$entry=='..'){
                  continue;
              }
              $Entry=$source.'/'.$entry;
              if(is_dir($Entry)){
                  backupOrRestoreWorld($Entry,$target.'/'.$entry);
                  continue;
              }
              copy($Entry,$target.'/'.$entry);
          }
          $d-&gt;close();
      }
      else{
          copy($source,$target);
      }
      if($source == "server/world"){
          return "World backed up.";
      }
      else {
          return "World restored from backup.";
      }
  }
  </code></pre>