[wp-trac] [WordPress Trac] #65150: Issue with copy_dir() function t does not skips folders.

WordPress Trac noreply at wordpress.org
Wed Apr 29 08:30:43 UTC 2026


#65150: Issue with copy_dir() function  t does not skips folders.
----------------------------+-----------------------------
 Reporter:  neo2k23         |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Filesystem API  |    Version:  trunk
 Severity:  normal          |   Keywords:
  Focuses:                  |
----------------------------+-----------------------------
 I have a issue with the copy_dir() function.  If i want to skip a folder
 called 'languages' it does not skip it and the files within that folder
 are still copied.

 $skip_list = array('languages');
 $result = copy_dir($from, $to, $skip_list);

 However if i revert copy_dir to the version which was in WordPress 3.2.1
 it works just fine. Below is the code from WordPress version 3.2.1. The
 languages and its content folder is skipped.


 {{{
         function copy_dir($from, $to, $skip_list = array() ) {
                 global $wp_filesystem;

                 $dirlist = $wp_filesystem->dirlist($from);

                 $from = trailingslashit($from);
                 $to = trailingslashit($to);

                 $skip_regex = '';
                 foreach ( (array)$skip_list as $key => $skip_file )
                         $skip_regex .= preg_quote($skip_file, '!') . '|';

                 if ( !empty($skip_regex) )
                         $skip_regex = '!(' . rtrim($skip_regex, '|') .
 ')$!i';

                 foreach ( (array) $dirlist as $filename => $fileinfo ) {
                         if ( !empty($skip_regex) )
                                 if ( preg_match($skip_regex, $from .
 $filename) )
                                         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, 0644);
                                         if ( ! $wp_filesystem->copy($from
 . $filename, $to . $filename, true, FS_CHMOD_FILE) )
                                                 return new
 WP_Error('copy_failed', __('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', __('Could not create directory.'), $to .
 $filename);
                                 }
                                 $result = copy_dir($from . $filename, $to
 . $filename, $skip_list);
                                 if ( is_wp_error($result) )
                                         return $result;
                         }
                 }
                 return true;
         }
 }}}


 The current dir_copy() function does not skip folders and its content.

 Can you please provide a fix for this as the description of dir_copy
 states it skips files/folders by the skip list.

 Or am i doing something wrong?  Do i have to provide the complete path to
 the languages folder?  In v3.2.1 we did not have todo this.

 Please advice

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/65150>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list