Monday, March 3, 2014

A simple copying directory function

function recurseCopy($src, $dst, $excludeFileType=array()) {
    $dir = opendir($src);
    mkdir($dst, 0755, true);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                $this->recurseCopy($src . '/' . $file, $dst . '/' . $file, $excludeFileType);
            }
            else {
                $filePathInfo = pathinfo($file);
                $ext = '';
                if(!empty($filePathInfo['extension'])){
                    $ext = $filePathInfo['extension'];
                }
                if(empty($ext) || !in_array($ext, $excludeFileType)){
                    copy($src . '/' . $file, $dst . '/' . $file);
                }
            }
        }
    }
    closedir($dir);
}

No comments: