On this page
copy_dir( string $from, string $to, string[] $skip_list = array() ): true|WP_Error
Copies a directory from one location to another via the WordPress Filesystem Abstraction.
Description
Assumes that WP_Filesystem() has already been called and setup.
Parameters
$fromstring Required-
Source directory.
$tostring Required-
Destination directory.
$skip_liststring[] Optional-
An array of files/folders to skip copying.
Default:
array()
Return
Source
File: wp-admin/includes/file.php. View all references
function copy_dir( $from, $to, $skip_list = array() ) {
global $wp_filesystem;
$dirlist = $wp_filesystem->dirlist( $from );
if ( false === $dirlist ) {
return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $to ) );
}
$from = trailingslashit( $from );
$to = trailingslashit( $to );
foreach ( (array) $dirlist as $filename => $fileinfo ) {
if ( in_array( $filename, $skip_list, true ) ) {
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 );
}
}
wp_opcache_invalidate( $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
Uses
| Uses | Description |
|---|---|
| wp_opcache_invalidate() wp-admin/includes/file.php | Attempts to clear the opcode cache for an individual PHP file. |
| copy_dir() wp-admin/includes/file.php | Copies a directory from one location to another via the WordPress Filesystem Abstraction. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_Upgrader::install_package() wp-admin/includes/class-wp-upgrader.php | Install a package. |
| update_core() wp-admin/includes/update-core.php | Upgrades the core of WordPress. |
| copy_dir() wp-admin/includes/file.php | Copies a directory from one location to another via the WordPress Filesystem Abstraction. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/copy_dir