wppaste
WordPress

copy_dir( string $from, string $to, string[] $skip_list = array() ): true|WP_Error

Since
2.5.0
Source
wp-admin/includes/file.php:2017
Copies a directory from one location to another via the WordPress Filesystem Abstraction.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
22–73

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 162.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
$dirlist === false22->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

PHPCompiledExecutedBranchesNotes
8.6-dev16222–7316
8.516222–7316
8.416222–73169 fewer instructions than PHP 8.3
8.317122–7616
8.217122–7616
8.117122–7616
7.417122–7616

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

Used by · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.