move_dir()
云策文档标注
概述
move_dir() 函数用于将目录从一个位置移动到另一个位置,支持递归操作和失败回退机制。它依赖于已初始化的 WP_Filesystem,并包含 OPcache 无效化处理。
关键要点
- 递归移动目录,成功时自动使 OPcache 无效化
- 如果重命名失败,会回退到 copy_dir() 进行复制操作
- 要求 WP_Filesystem() 已调用并设置,不用于合并目录
- 参数包括源目录、目标目录和可选的覆盖标志
- 返回 true 或 WP_Error 对象,指示操作结果
代码示例
// Connecting to the filesystem.
if ( ! WP_Filesystem() ) {
// Unable to connect to the filesystem, FTP credentials may be required or something.
exit;
}
global $wp_filesystem;
// From Directory Path
$from = '/path/to/from_folder_name';
// To Directory Path
$to = '/path/to/to_folder_name';
$result = move_dir($from, $to);注意事项
- 源和目标路径相同会返回 WP_Error
- 目标目录存在且未设置覆盖时返回错误
- 移动成功后延迟 200ms 以更新文件系统缓存,防止环境问题
- 回退到 copy_dir() 时,会尝试创建目标目录并清理源目录
原文内容
Moves a directory from one location to another.
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.
Parameters
$fromstringrequired-
Source directory.
$tostringrequired-
Destination directory.
$overwritebooloptional-
Whether to overwrite the destination directory if it exists.
Default:
false
Source
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
| Version | Description |
|---|---|
| 6.2.0 | Introduced. |
Skip to note 2 content
Shail Mehta
// Connecting to the filesystem. if ( ! WP_Filesystem() ) { // Unable to connect to the filesystem, FTP credentials may be required or something. exit; } global $wp_filesystem; // From Directory Path $from = '/path/to/from_folder_name'; // To Directory Path $to = '/path/to/to_folder_name'; $result = move_dir($from, $to);