{"id":315,"date":"2017-02-11T23:33:45","date_gmt":"2017-02-11T22:33:45","guid":{"rendered":"http:\/\/lab.fawno.com\/?p=315"},"modified":"2022-03-07T15:53:10","modified_gmt":"2022-03-07T14:53:10","slug":"","status":"publish","type":"post","link":"https:\/\/lab.fawno.com\/en\/2017\/02\/11\/mover-un-directorio\/","title":{"rendered":"","raw":""},"content":{"rendered":"","protected":false,"raw":""},"excerpt":{"rendered":"","protected":false,"raw":""},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_editorskit_title_hidden":false,"_editorskit_reading_time":1,"_editorskit_typography_data":[],"_editorskit_blocks_typography":"","_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_es_post_content":"\n
Para una tonter\u00eda que estaba haciendo he necesitado renombrar (aka mover) un directorio de una ruta a otra. Como no existe la funci\u00f3n rename<\/a><\/em> orientada a directorios, he tenido que crearla:<\/p>\n\n\n\n No hay mucho que decir, salvo que no hace comprobaciones muy exhaustivas y hay que tener cierto cuidado al utilizarla.<\/p>\n\n\n\nfunction path_rename ($from, $to) {\n if (is_dir($from)) {\n path_check($to, 0777);\n $exclude = ['', '.', '..'];\n foreach (array_diff(scandir($from), $exclude) as $file) {\n if (is_file($from . DIRECTORY_SEPARATOR . $file)) {\n rename($from . DIRECTORY_SEPARATOR . $file, $to . DIRECTORY_SEPARATOR . $file);\n } else {\n path_rename($from . DIRECTORY_SEPARATOR . $file, $to . DIRECTORY_SEPARATOR . $file);\n }\n }\n rmdir($from);\n }\n}<\/pre>\n\n\n\n