Deletes a directory and its contents recursively / deeply.
<?php
/**
* function rmdirr
* Copyright (C) 2006 Dao Gottwald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contact information:
* Dao Gottwald <dao at design-noir.de>
* Herltestraße 12
* D-01307, Germany
*
* @version 1.1
*/
function rmdirr ($dir) {
if (is_dir ($dir) && !is_link ($dir)) {
return cleardir ($dir) ? rmdir ($dir) : false;
}
return unlink ($dir);
}
?>
Empties a directory, deleting all contained subdirectories and files deeply.
<?php
/**
* function cleardir
* Copyright (C) 2006 Dao Gottwald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contact information:
* Dao Gottwald <dao at design-noir.de>
* Herltestraße 12
* D-01307, Germany
*
* @version 1.0
*/
function cleardir ($dir) {
if (!($dir = dir ($dir))) {
return false;
}
while (false !== $item = $dir->read()) {
if ($item != '.' && $item != '..' && !rmdirr ($dir->path . DIRECTORY_SEPARATOR . $item)) {
$dir->close();
return false;
}
}
$dir->close();
return true;
}
?>
while($file=readdir($directorio))| will fail on a file or directory named “0”.