使用PHP将文件夹重命名为子文件夹 [英] rename folder into sub-folder with PHP

查看:95
本文介绍了使用PHP将文件夹重命名为子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过重命名移动文件夹。 test1和test2文件夹已经存在。

I'm trying to move a folder by renaming it. Both the test1 and test2 folders already exist.

rename(
 "test1",
 "test2/xxx1/xxx2"
);

我得到的错误是: rename(...):没有这样的文件或目录

我认为这是因为目录 xxx1不存在。
我该如何移动test1目录?

I assume this is because the directory "xxx1" does not exist. How can I move the test1 directory anyway?

推荐答案

您可能需要创建要进入的目录,例如

You might need to create the directory it is going into, e.g.

$toName = "test2/xxx1/xxx2";

if (!is_dir(dirname($toName))) {
    mkdir(dirname($toName), 0777, true);
}

rename("test1", $toName);

mkdir() 是递归的,这意味着您可以一次调用创建嵌套目录。

The third parameter to mkdir() is 'recursive', which means you can create nested directories with one call.

这篇关于使用PHP将文件夹重命名为子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆