/**
 * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and setup.
 *
 * This is a temporary function for the 3.1 -> 3.2 upgrade only and will be removed in 3.3
 *
 * @ignore
 * @since 3.2.0
 * @see copy_dir()
 *
 * @param string $from source directory
 * @param string $to destination directory
 * @param array $skip_list a list of files/folders to skip copying
 * @return mixed WP_Error on failure, True on success.
 */
function _copy_dir($from, $to, $skip_list = array() ) {
	global $wp_filesystem;

	$dirlist = $wp_filesystem->dirlist($from);

	$from = trailingslashit($from);
	$to = trailingslashit($to);

	$skip_regex = '';
	foreach ( (array)$skip_list as $key => $skip_file )
		$skip_regex .= preg_quote($skip_file, '!') . '|';

	if ( !empty($skip_regex) )
		$skip_regex = '!(' . rtrim($skip_regex, '|') . ')$!i';

	foreach ( (array) $dirlist as $filename => $fileinfo ) {
		if ( !empty($skip_regex) )
			if ( preg_match($skip_regex, $from . $filename) )
				continue;

		if ( 'f' == $fileinfo['type'] ) {
			if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
				// If copy failed, chmod file to 0644 and try again.
				$wp_filesystem->chmod($to . $filename, 0644);
				if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) )
					return new WP_Error('copy_failed', __('Could not copy file.'), $to . $filename);
			}
		} elseif ( 'd' == $fileinfo['type'] ) {
			if ( !$wp_filesystem->is_dir($to . $filename) ) {
				if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
					return new WP_Error('mkdir_failed', __('Could not create directory.'), $to . $filename);
			}
			$result = _copy_dir($from . $filename, $to . $filename, $skip_list);
			if ( is_wp_error($result) )
				return $result;
		}
	}
	return true;
}
Exemplo n.º 2
0
/**
 * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and setup.
 *
 * This is a temporary function for the 3.1 -> 3.2 upgrade, as well as for those upgrading to
 * 3.7+
 *
 * @ignore
 * @since 3.2.0
 * @since 3.7.0 Updated not to use a regular expression for the skip list
 * @see copy_dir()
 *
 * @global WP_Filesystem_Base $wp_filesystem
 *
 * @param string $from     source directory
 * @param string $to       destination directory
 * @param array $skip_list a list of files/folders to skip copying
 * @return mixed WP_Error on failure, True on success.
 */
function _copy_dir($from, $to, $skip_list = array())
{
    global $wp_filesystem;
    $dirlist = $wp_filesystem->dirlist($from);
    $from = trailingslashit($from);
    $to = trailingslashit($to);
    foreach ((array) $dirlist as $filename => $fileinfo) {
        if (in_array($filename, $skip_list)) {
            continue;
        }
        if ('f' == $fileinfo['type']) {
            if (!$wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE)) {
                // If copy failed, chmod file to 0644 and try again.
                $wp_filesystem->chmod($to . $filename, FS_CHMOD_FILE);
                if (!$wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE)) {
                    return new WP_Error('copy_failed__copy_dir', __('Could not copy file.'), $to . $filename);
                }
            }
        } elseif ('d' == $fileinfo['type']) {
            if (!$wp_filesystem->is_dir($to . $filename)) {
                if (!$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR)) {
                    return new WP_Error('mkdir_failed__copy_dir', __('Could not create directory.'), $to . $filename);
                }
            }
            /*
             * Generate the $sub_skip_list for the subdirectory as a sub-set
             * of the existing $skip_list.
             */
            $sub_skip_list = array();
            foreach ($skip_list as $skip_item) {
                if (0 === strpos($skip_item, $filename . '/')) {
                    $sub_skip_list[] = preg_replace('!^' . preg_quote($filename, '!') . '/!i', '', $skip_item);
                }
            }
            $result = _copy_dir($from . $filename, $to . $filename, $sub_skip_list);
            if (is_wp_error($result)) {
                return $result;
            }
        }
    }
    return true;
}
Exemplo n.º 3
0
function _copy_dir($dir_name, $new_dir)
{
    if (!is_dir($new_dir)) {
        mkdir($new_dir, 0777, true);
    }
    if (!(is_dir($dir_name) && ($dh = opendir($dir_name)))) {
        return true;
    }
    while (($file = readdir($dh)) !== false) {
        if (substr($file, 0, 1) == '.') {
            continue;
        }
        if (is_dir($dir_name . '/' . $file)) {
            _copy_dir($dir_name . '/' . $file, $new_dir . '/' . $file);
        } elseif (file_exists($new_dir . '/' . $file) && file_get_contents($dir_name . '/' . $file) != file_get_contents($new_dir . '/' . $file)) {
            echo <<<END_of_TEXT
"{$new_dir}/{$file}" already exists.

END_of_TEXT;
        } else {
            copy($dir_name . '/' . $file, $new_dir . '/' . $file);
        }
    }
    closedir($dh);
    return true;
}