move_dir( string $from, string $to, bool $overwrite = false ): true|WP_Error
- Since
- 6.2.0
- Source
wp-admin/includes/file.php:2106
Description
Recursively invalidates OPcache on success.
If the renaming failed, falls back to copy_dir().
Assumes that WP_Filesystem() has already been called and setup.
This function is not designed to merge directories, copy_dir() should be used instead.
Compatibility
- WordPress
- since 6.2.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.
$overwritebooloptional- Whether to overwrite the destination directory if it exists.
Default false.Default:false
Return value
true|WP_Error- True on success, WP_Error on failure.
Performance profile
How much work a call to move_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
- Heavy
- Scaling
- Constant
- Instructions
- 26–60
- Plugin surface
- None
- Called by
- 3
Reaches the database via ->delete().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 101.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- sqldatabase query
->delete()called directly
Further down the call graph this can also reach hook, 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 · 15 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost move_dir() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 26 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), __() |
->exists() | 32 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), __() |
!->exists() && ->move() | 34 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->move(), usleep(), wp_opcache_invalidate_directory() |
->exists() && !->delete() | 36 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), __() |
->exists() && ->delete() && ->move() | 40 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), ->move(), usleep(), wp_opcache_invalidate_directory() |
!->exists() && !->move() && ->is_dir() && $result !== true | 44 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->move(), ->is_dir(), basename() |
!->exists() && !->move() && !->is_dir() && !->mkdir() | 46 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->move(), ->is_dir(), ->mkdir(), __() |
!->exists() && !->move() && ->is_dir() && $result === true | 48 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->move(), ->is_dir(), basename(), ->delete() |
!->exists() && !->move() && !->is_dir() && ->mkdir() && $result !== true | 50 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->move(), ->is_dir(), ->mkdir(), basename() |
->exists() && ->delete() && !->move() && ->is_dir() && $result !== true | 50 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), ->move(), ->is_dir(), basename() |
->exists() && ->delete() && !->move() && !->is_dir() && !->mkdir() | 52 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), ->move(), ->is_dir(), ->mkdir(), __() |
!->exists() && !->move() && !->is_dir() && ->mkdir() && $result === true | 54 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->move(), ->is_dir(), ->mkdir(), basename(), ->delete() |
3 further outcomes, up to 60 instructions
->exists() && ->delete() && !->move() && ->is_dir() && $result === true | 54 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), ->move(), ->is_dir(), basename(), ->delete() |
->exists() && ->delete() && !->move() && !->is_dir() && ->mkdir() && $result !== true | 56 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), ->move(), ->is_dir(), ->mkdir(), basename() |
->exists() && ->delete() && !->move() && !->is_dir() && ->mkdir() && $result === true | 60 | strtolower(), trailingslashit(), strtolower(), trailingslashit(), ->exists(), ->delete(), ->move(), ->is_dir(), ->mkdir(), basename(), ->delete() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 101 instructions, 26–60 executed per call, 8 branches. The work does not change between versions.
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 · 5
- trailingslashit()Appends a trailing slash.
- __()Retrieves the translation of $text.
- wp_opcache_invalidate_directory()Attempts to clear the opcode cache for a directory of files.
- copy_dir()Copies a directory from one location to another via the WordPress Filesystem Abstraction.
- WP_Error::__construct()Initializes the error.
Used by · 3
- WP_Upgrader::install_package()Install a package.
- WP_Upgrader::move_to_temp_backup_dir()Moves the plugin or theme being updated into a temporary backup directory.
- WP_Upgrader::restore_temp_backup()Restores the plugin or theme from temporary backup.
Source code
function move_dir( $from, $to, $overwrite = false ) { global $wp_filesystem; if ( trailingslashit( strtolower( $from ) ) === trailingslashit( strtolower( $to ) ) ) { return new WP_Error( 'source_destination_same_move_dir', __( 'The source and destination are the same.' ) ); } if ( $wp_filesystem->exists( $to ) ) { if ( ! $overwrite ) { return new WP_Error( 'destination_already_exists_move_dir', __( 'The destination folder already exists.' ), $to ); } elseif ( ! $wp_filesystem->delete( $to, true ) ) { // Can't overwrite if the destination couldn't be deleted. return new WP_Error( 'destination_not_deleted_move_dir', __( 'The destination directory already exists and could not be removed.' ) ); } } if ( $wp_filesystem->move( $from, $to ) ) { /* * When using an environment with shared folders, * there is a delay in updating the filesystem's cache. * * This is a known issue in environments with a VirtualBox provider. * * A 200ms delay gives time for the filesystem to update its cache, * prevents "Operation not permitted", and "No such file or directory" warnings. * * This delay is used in other projects, including Composer. * @link https://github.com/composer/composer/blob/2.5.1/src/Composer/Util/Platform.php#L228-L233 */ usleep( 200000 ); wp_opcache_invalidate_directory( $to ); return true; } // Fall back to a recursive copy. if ( ! $wp_filesystem->is_dir( $to ) ) { if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) { return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to ); } } $result = copy_dir( $from, $to, array( basename( $to ) ) ); // Clear the source directory. if ( true === $result ) { $wp_filesystem->delete( $from, true ); } return $result;}Changelog
Introduced in 6.2.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.