copy_dir( string $from, string $to, string[] $skip_list = array() ): true|WP_Error
- Since
- 2.5.0
- Source
wp-admin/includes/file.php:2022
Description
Assumes that WP_Filesystem() has already been called and setup.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$fromstring- Source directory.
$tostring- Destination directory.
$skip_liststring[]optional- An array of files/folders to skip copying.Default:
array()
Return value
true|WP_Error- True on success, WP_Error on failure.
Performance profile
How much work a call to copy_dir() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 22–73
- Plugin surface
- None
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 162.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below copy_dir()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost copy_dir() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$dirlist === false | 22 | ->dirlist(), __(), basename() |
$dirlist !== false && ->exists() | 26–27 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists() |
$dirlist !== false && !->exists() && ->mkdir() | 30–31 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->mkdir() |
$dirlist !== false && !->exists() && !->mkdir() | 38 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->mkdir(), __(), basename() |
$dirlist !== false && ->exists() && !$dirlist && !$filename && ->is_dir() && is_wp_error() | 57–58 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->is_dir(), copy_dir(), is_wp_error() |
$dirlist !== false && ->exists() && !$dirlist && !$filename && !->is_dir() && !->mkdir() | 58 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->is_dir(), ->mkdir(), __() |
$dirlist !== false && !->exists() && ->mkdir() && !$dirlist && !$filename && ->is_dir() && is_wp_error() | 61–62 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->mkdir(), ->is_dir(), copy_dir(), is_wp_error() |
$dirlist !== false && !->exists() && !$dirlist && !$filename && !->is_dir() | 62 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->mkdir(), ->is_dir(), ->mkdir(), __() |
$dirlist !== false && ->exists() && !$dirlist && !$filename && !->is_dir() && ->mkdir() && is_wp_error() | 64–65 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->is_dir(), ->mkdir(), copy_dir(), is_wp_error() |
$dirlist !== false && !->exists() && ->mkdir() && !$dirlist && !$filename && !->is_dir() && is_wp_error() | 68–69 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->mkdir(), ->is_dir(), ->mkdir(), copy_dir(), is_wp_error() |
$dirlist !== false && ->exists() && !$dirlist && !$filename && !->copy() | 69 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->copy(), ->chmod(), ->copy(), __() |
$dirlist !== false && !->exists() && ->mkdir() && !$dirlist && !$filename && !->copy() | 73 | ->dirlist(), trailingslashit(), trailingslashit(), ->exists(), ->mkdir(), ->copy(), ->chmod(), ->copy(), __() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 162 | 22–73 | 16 | |
| 8.5 | 162 | 22–73 | 16 | |
| 8.4 | 162 | 22–73 | 16 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 171 | 22–76 | 16 | |
| 8.2 | 171 | 22–76 | 16 | |
| 8.1 | 171 | 22–76 | 16 | |
| 7.4 | 171 | 22–76 | 16 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 7
- __()Retrieves the translation of $text.
- trailingslashit()Appends a trailing slash.
- wp_opcache_invalidate()Attempts to clear the opcode cache for an individual PHP file.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- copy_dir()Copies a directory from one location to another via the WordPress Filesystem Abstraction.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- WP_Error::__construct()Initializes the error.
Used by · 4
- WP_Upgrader::install_package()Install a package.
- copy_dir()Copies a directory from one location to another via the WordPress Filesystem Abstraction.
- move_dir()Moves a directory from one location to another.
- update_core()Upgrades the core of WordPress.
Source code
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( $from ) ); } $from = trailingslashit( $from ); $to = trailingslashit( $to ); if ( ! $wp_filesystem->exists( $to ) && ! $wp_filesystem->mkdir( $to ) ) { return new WP_Error( 'mkdir_destination_failed_copy_dir', __( 'Could not create the destination directory.' ), basename( $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 ( str_starts_with( $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;}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.