^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数copy_dir()用法示例

copy_dir( string $from,  string $to,  array $skip_list = array() )

Copies a directory from one location to another via the WordPress Filesystem Abstraction.


描述

Assumes that WP_Filesystem() has already been called and setup.


参数

$from

(string)
(Required)
源代码 directory

$to

(string)
(Required)
destination directory

$skip_list

(array)
(Optional)
a list of files/folders to skip copying

Default value: array()


返回值

(mixed) WP_Error on failure, True on success.


源代码

File: wp-admin/includes/file.php

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;
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-admin/includes/file.php:
    copy_dir()
  • wp-includes/l10n.php:
    __()
  • wp-includes/formatting.php:
    trailingslashit()
  • wp-includes/load.php:
    is_wp_error()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()

Used By

  • wp-admin/includes/class-wp-upgrader.php:
    WP_Upgrader::install_package()
  • wp-admin/includes/update-core.php:
    update_core()
  • wp-admin/includes/file.php:
    copy_dir()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by J.D. Grimes

    
    // Connecting to the filesystem.
    if ( ! WP_Filesystem() ) {
    	// Unable to connect to the filesystem, FTP credentials may be required or something.
    	// You can request these with request_filesystem_credentials()
    	exit;
    }
    
    // Don't forget that the target directory needs to exist.
    // If it doesn't already, you'll need to create it.
    global $wp_filesystem;
    $wp_filesystem->mkdir( $target_dir );
    
    // Now copy all the files in the 源代码 directory to the target directory.
    copy_dir( $src_dir, $target_dir );
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈