{"id":11,"date":"2015-05-02T03:26:40","date_gmt":"2015-05-02T01:26:40","guid":{"rendered":"http:\/\/lab.fawno.com\/?p=11"},"modified":"2022-03-07T00:48:59","modified_gmt":"2022-03-06T23:48:59","slug":"","status":"publish","type":"post","link":"https:\/\/lab.fawno.com\/en\/2015\/05\/02\/borrar-un-directorio-de-forma-recursiva\/","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":0,"_editorskit_typography_data":[],"_editorskit_blocks_typography":"","_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_es_post_content":"\n

En ocasiones quieres borrar desde PHP un directorio contenga lo que contenga... para esa tarea cree esta funci\u00f3n:<\/p>\n\n\n\n

function rd ($path) {\n  foreach (array_diff(scandir($path), ['', '.', '..']) as $file) {\n    if (is_file($path . DIRECTORY_SEPARATOR . $file)) {\n      unlink($path . DIRECTORY_SEPARATOR . $file);\n    } else {\n      rd($path . DIRECTORY_SEPARATOR . $file);\n    }\n  }\n  rmdir($path);\n}<\/pre>\n\n\n\n

Hace a\u00f1os que la cree y por tanto seguro que es mejorable, pero cumple su funci\u00f3n a la perfecci\u00f3n.<\/p>\n\n\n\n

Actualizaci\u00f3n (2017-02-11)<\/strong>: Utilizo la constante DIRECTORY_SEPARATOR<\/a> para construir las rutas, lo que evidentemente es m\u00e1s vers\u00e1til y nos evitar\u00e1 posibles errores.<\/p>\n\n\n\n

Saludos<\/p>\n","_es_post_name":"borrar-un-directorio-de-forma-recursiva","_es_post_excerpt":"","_es_post_title":"Borrar un directorio de forma recursiva","_en_post_content":"","_en_post_name":"","_en_post_excerpt":"","_en_post_title":"","edit_language":"en","footnotes":""},"categories":[5,3],"tags":[6],"ninja_gutenberg_blocks_featured_media_urls":{"thumbnail":"","ninja_gutenberg_blocks_landscape_large":"","ninja_gutenberg_blocks_portrait_large":"","ninja_gutenberg_blocks_square_large":"","ninja_gutenberg_blocks_landscape":"","ninja_gutenberg_blocks_portrait":"","ninja_gutenberg_blocks_square":"","full":""},"_links":{"self":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":41,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":1842,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/11\/revisions\/1842"}],"wp:attachment":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}