rename folder into sub-folder with PHP

前端 未结 4 1956
醉酒成梦
醉酒成梦 2021-02-19 09:50

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

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

The e

4条回答
  •  無奈伤痛
    2021-02-19 10:30

    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);
    

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

提交回复
热议问题