WordPress at a glance

copy_dir() WP 2.5.0

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

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

The functionality of this function is based on the file system API which is based on the core class WP_Filesystem_Base. This core class is extended by the other class suitable for the server your site works on. The resulting object is placed in the global variable $wp_filesystem.

To get the list of possible methods of $wp_filesystem, see the core class:

and other classes that inherit it:

No Hooks.

Return

true/WP_Error. True on success, WP_Error on failure.

Usage

copy_dir( $from, $to, $skip_list );
$from(string) (required)
Source directory.
$to(string) (required)
Destination directory.
$skip_list(array)
A list of files/folders to skip copying.
Default: array()

Examples

#1 Copy one directory to another

Copy avatars directory from uploads to its subdirectory with the number of the current month.

global $wp_filesystem;

$dir = wp_get_upload_dir();
/*
Array(
	[path]    => /home/kama.ru/html/wp-content/uploads/2018/05
	[url]     => //kama.ru/wp-content/uploads/2018/05
	[subdir]  => /2018/05
	[basedir] => /home/kama.ru/html/wp-content/uploads
	[baseurl] => https://wp-kama.ru/wp-content/uploads
	[error]   => 
)
*/

$from = $dir['basedir'] .'/avatars';
// $to can't be a direct path, because so FTP and SSH won't work
$to   = $wp_filesystem->find_folder( $dir['path'] );

copy_dir( $from, $to );

#1.2 Skip some files

The same, but skip index.php, index.html files.

global $wp_filesystem;

$dir =  wp_get_upload_dir();

$from = $dir['basedir'] .'/avatars';
// $to can't be a direct path, because so FTP and SSH won't work
$to   = $wp_filesystem->find_folder( $dir['path'] );

$skip_list = array( 'index.php', 'index.html' );

copy_dir( $from, $to, $skip_list );

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 2.5.0 Introduced.

Code of copy dir: wp-admin/includes/file.php WP 5.2.4

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

Related Functions

From tag: File system (create delete files folders)

No comments
    Hello, !     Log In . Register