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.