de

designoir

rmdirr & cleardir

rmdirr.php

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);
}

?>

cleardir.php

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;
}

?>

Comments

  1. David wrote on February 27, 2006 10:39 AM GMT ():
    Hello a thanks:)

    i tried to write my own function like yours, but it allways failed:(
    Thanks 4 help

    gr8 stuff:)

    Have a nice day and sleep well at night
  2. K-roy wrote on March 6, 2006 07:05 PM GMT ():
    Just B E A Utifull :)
    Thanx alot…
  3. Andras wrote on March 7, 2006 02:13 AM GMT ():
    I’ve already done my function to find & destroy any directories.. I need this beacuse dreamweaver always creates “_notes” and it doesnt stop when i set him. The problem is thata i always get a Permission denied even if the directory is clear:

    function scan($filename,$path,$delete){
    $directorio=opendir($path);
    while($file=readdir($directorio)){
    if($file!==“.” && $file!==“..”){
    if(is_dir(“$path/$file”)){
    if($file==$filename){
    scan($filename,“$path/$file”,true);
    rmdir(“$path/$file”)
    }else scan($filename,“$path/$file”,false);
    }else{
    if($delete==true){
    unlink(“$path/$file”);
    echo “\n<br> Eliminado exitosamente: $file”;
    };
    };
    };
    };
    };
  4. Dao wrote on March 7, 2006 03:21 AM GMT ():
    Andras: So probably the user that the web server runs as really has no writing permission. Also note that |while($file=readdir($directorio))| will fail on a file or directory named “0”.
  5. Julie Fuller wrote on March 27, 2006 08:18 PM BST ():
    Is this free to use, GPL, LGPL, what?

    Thanks.
  6. Julie Fuller wrote on March 27, 2006 09:17 PM BST ():
    I’m going crazy if the license information was there a few hours ago – I distinctly remember there *not* being a scrollbar on the top box… ::wondering about own sanity::

    At any rate, thanks for the answer, and sorry if I’m just really dazey today and didn’t notice it earlier! :-)
  7. Dao wrote on March 27, 2006 09:25 PM BST ():
    It’s licensed under the LGPL now. If you downloaded the source before, consider that public domain. ;-)
  8. waz wrote on May 26, 2006 07:25 PM BST ():
    Nice script. I added a little test to rmdirr

    if ( file_exists($dir) )
    { return unlink ($dir); }

    to avoid warnings if for some reason the file does not exist
  9. Rogee wrote on July 26, 2006 03:07 PM BST ():
    Great script! Worked like a charm, even on an IIS server. Thank you.
  10. ieter wrote on August 22, 2006 03:10 PM BST ():
    To avoid infinite loops because of symlinks, I changed the following in rmdirr:

    if (is_dir ($dir))

    =>

    if (is_dir($dir) && !is_link($dir))
  11. Dao wrote on August 23, 2006 09:41 AM BST ():
    Thanks, ieter. That makes sense. I’ve modified rmdirr accordingly.
  12. Joachim Mueller wrote on January 15, 2009 08:12 AM GMT ():
    Too bad your functions are PHP5 only - they looked very promising. I needed corresponding functions for use on a GPLed project with minimum requirements PHP4.

HTML will not be interpreted.

Last changed on July 25, 2006 Dão G., 2005–2010
aggressiv akt andromeda bar beine blue efeu frontal fugaetu industriell komet land noir rost rot sonnenblume splash split winter wolke zeit