Cómo eliminar una carpeta con contenido usando PHP [duplicar]
Esta pregunta ya tiene una respuesta aquí:
- Eliminar directorio con archivos en él? 30 respuestas
Necesito eliminar una carpeta con contenido usando PHP. rmdir()
y unlink()
eliminan las carpetas vacías, pero no pueden eliminar las carpetas que tienen contenido.
6 answers
Esta función le permitirá eliminar cualquier carpeta (siempre y cuando se pueda escribir) y sus archivos y subdirectorios.
function Delete($path)
{
if (is_dir($path) === true)
{
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
Delete(realpath($path) . '/' . $file);
}
return rmdir($path);
}
else if (is_file($path) === true)
{
return unlink($path);
}
return false;
}
O sin recursión usando RecursiveDirectoryIterator
:
function Delete($path)
{
if (is_dir($path) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file)
{
if (in_array($file->getBasename(), array('.', '..')) !== true)
{
if ($file->isDir() === true)
{
rmdir($file->getPathName());
}
else if (($file->isFile() === true) || ($file->isLink() === true))
{
unlink($file->getPathname());
}
}
}
return rmdir($path);
}
else if ((is_file($path) === true) || (is_link($path) === true))
{
return unlink($path);
}
return false;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-04 10:59:01
Debe recorrer el contenido de la carpeta (incluido el contenido de cualquier subcarpeta) y eliminarlos primero.
Aquí hay un ejemplo: http://lixlpixel.org/recursive_function/php/recursive_directory_delete /
Tenga cuidado con él!!!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-26 12:43:43
Aquí hay un script que hará justo lo que necesita:
/**
* Recursively delete a directory
*
* @param string $dir Directory name
* @param boolean $deleteRootToo Delete specified top-level directory as well
*/
function unlinkRecursive($dir, $deleteRootToo)
{
if(!$dh = @opendir($dir))
{
return;
}
while (false !== ($obj = readdir($dh)))
{
if($obj == '.' || $obj == '..')
{
continue;
}
if (!@unlink($dir . '/' . $obj))
{
unlinkRecursive($dir.'/'.$obj, true);
}
}
closedir($dh);
if ($deleteRootToo)
{
@rmdir($dir);
}
return;
}
Lo obtuve de php.net y funciona.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-26 12:46:05
No hay una sola función construida en PHP que permita esto, tienes que escribir la tuya propia con rmdir y desvincular.
Un ejemplo (tomado de un comentario sobre php.net docs):
<?
// ensure $dir ends with a slash
function delTree($dir) {
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == '/' )
delTree( $file );
else
unlink( $file );
}
rmdir( $dir );
}
?>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-26 12:46:02
Tendrás que eliminar todos los archivos recursivamente. Hay muchas funciones de ejemplo en los comentarios de la página de manual rmdir
:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-26 12:45:08
Siempre se puede hacer trampa y hacer
shell_exec("rm -rf /path/to/folder");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-26 12:44:56