copy_dir

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

Description

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

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

  1. if ( !function_exists( 'copy_dir' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/file.php'; 
  3.  
  4. // source directory 
  5. $from = ''; 
  6.  
  7. // destination directory 
  8. $to = ''; 
  9.  
  10. // a list of files/folders to skip copying 
  11. $skip_list = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = copy_dir($from, $to, $skip_list); 
  15.  

Defined (2)

The function is defined in the following location(s).

/wp-admin/includes/file.php  
  1. function copy_dir($from, $to, $skip_list = array() ) { 
  2. global $wp_filesystem; 
  3.  
  4. $dirlist = $wp_filesystem->dirlist($from); 
  5.  
  6. $from = trailingslashit($from); 
  7. $to = trailingslashit($to); 
  8.  
  9. foreach ( (array) $dirlist as $filename => $fileinfo ) { 
  10. if ( in_array( $filename, $skip_list ) ) 
  11. continue; 
  12.  
  13. if ( 'f' == $fileinfo['type'] ) { 
  14. if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) { 
  15. // If copy failed, chmod file to 0644 and try again. 
  16. $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE ); 
  17. if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) 
  18. return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename ); 
  19. } elseif ( 'd' == $fileinfo['type'] ) { 
  20. if ( !$wp_filesystem->is_dir($to . $filename) ) { 
  21. if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) ) 
  22. return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename ); 
  23.  
  24. // generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list 
  25. $sub_skip_list = array(); 
  26. foreach ( $skip_list as $skip_item ) { 
  27. if ( 0 === strpos( $skip_item, $filename . '/' ) ) 
  28. $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item ); 
  29.  
  30. $result = copy_dir($from . $filename, $to . $filename, $sub_skip_list); 
  31. if ( is_wp_error($result) ) 
  32. return $result; 
  33. return true; 
/wp-admin/includes/update-core.php  
  1. function _copy_dir($from, $to, $skip_list = array() ) { 
  2. global $wp_filesystem; 
  3.  
  4. $dirlist = $wp_filesystem->dirlist($from); 
  5.  
  6. $from = trailingslashit($from); 
  7. $to = trailingslashit($to); 
  8.  
  9. foreach ( (array) $dirlist as $filename => $fileinfo ) { 
  10. if ( in_array( $filename, $skip_list ) ) 
  11. continue; 
  12.  
  13. if ( 'f' == $fileinfo['type'] ) { 
  14. if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) { 
  15. // If copy failed, chmod file to 0644 and try again. 
  16. $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE ); 
  17. if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) 
  18. return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename ); 
  19. } elseif ( 'd' == $fileinfo['type'] ) { 
  20. if ( !$wp_filesystem->is_dir($to . $filename) ) { 
  21. if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) ) 
  22. return new WP_Error( 'mkdir_failed__copy_dir', __( 'Could not create directory.' ), $to . $filename ); 
  23.  
  24. /** 
  25. * Generate the $sub_skip_list for the subdirectory as a sub-set 
  26. * of the existing $skip_list. 
  27. */ 
  28. $sub_skip_list = array(); 
  29. foreach ( $skip_list as $skip_item ) { 
  30. if ( 0 === strpos( $skip_item, $filename . '/' ) ) 
  31. $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item ); 
  32.  
  33. $result = _copy_dir($from . $filename, $to . $filename, $sub_skip_list); 
  34. if ( is_wp_error($result) ) 
  35. return $result; 
  36. return true;