Para una tontería que estaba haciendo he necesitado renombrar (aka mover) un directorio de una ruta a otra. Como no existe la función rename orientada a directorios, he tenido que crearla:
function path_rename ($from, $to) {
if (is_dir($from)) {
path_check($to, 0777);
$exclude = ['', '.', '..'];
foreach (array_diff(scandir($from), $exclude) as $file) {
if (is_file($from . DIRECTORY_SEPARATOR . $file)) {
rename($from . DIRECTORY_SEPARATOR . $file, $to . DIRECTORY_SEPARATOR . $file);
} else {
path_rename($from . DIRECTORY_SEPARATOR . $file, $to . DIRECTORY_SEPARATOR . $file);
}
}
rmdir($from);
}
}
No hay mucho que decir, salvo que no hace comprobaciones muy exhaustivas y hay que tener cierto cuidado al utilizarla.
ATENCIÓN: Utiliza la función path_check que he descrito anteriormente.
Compártelo