WordPress函数:move_dir 移动文件夹

江河/ 2023年04月07日/ WordPress/ 浏览 1140

WordPress 6.2 新增了一个移动文件夹函数 move_dir。


函数原型:


move_dir( string $from, string $to, bool $overwrite = false ): true|WP_Error


如果,目标文件夹不存在,就直接移动原文件夹到目标文件夹。


如果,目标文件夹已存在, 当参数 $overwrite 为 false 时,函数会失败;当$overwrite 为 true 时,会先删除目标文件夹,再把原文件夹移过去。


如果,希望原文件夹和目标文件夹合并,可以使用 copy_dir 函数,可以参考:《WordPress函数:copy_dir 复制文件夹》。使用 copy_dir 同样会有同名文件覆盖的问题。move_dir 和 copy_dir 功能强大,使用当谨慎。


使用举例:


if (!WP_Filesystem()) {
    exit;
}

$td =  get_template_directory();
move_dir($td . '/from', $td . '/to', true);


使用 move_dir 需要先调用 WP_Filesystem() 。


发表评论

暂无评论,抢个沙发...

客服 工单