I'm trying to move a folder by renaming it. Both the test1 and test2 folders already exist.
rename(
"test1",
"test2/xxx1/xxx2"
);
The error I get is: rename(...): No such file or directory
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);
The third parameter to mkdir()
is 'recursive', which means you can create nested directories with one call.
Why not make sure all parent directories exist first, by making them? mkdir - use the recursive parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With