copy_dir
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 (3)
- 0. $from (string)
- Source directory
- 1. $to (string)
- Destination directory
- 2. $skip_list — Optional. (array) =>
array()
- A list of files/folders to skip copying
Usage
if ( !function_exists( 'copy_dir' ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; } // source directory $from = ''; // destination directory $to = ''; // a list of files/folders to skip copying $skip_list = array(); // NOTICE! Understand what this does before running. $result = copy_dir($from, $to, $skip_list);
Defined (2)
The function is defined in the following location(s).
- /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) )
- }
- } elseif ( 'd' == $fileinfo['type'] ) {
- if ( !$wp_filesystem->is_dir($to . $filename) ) {
- if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
- }
- // 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;
- }
- /wp-admin/includes/update-core.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) )
- }
- } elseif ( 'd' == $fileinfo['type'] ) {
- if ( !$wp_filesystem->is_dir($to . $filename) ) {
- if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
- }
- /**
- * 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;
- }