public static function remove($path, $deleteDir = true)
    {
        // minimum security check
        if ($path == '' || $path == '/' || $path == DIRECTORY_SEPARATOR) {
            throw new \InvalidArgumentException('The root cannot be removed !!');
        }

        if (!file_exists($path)) {
            return true;
        }

        $dir = new \DirectoryIterator($path);
        foreach ($dir as $dirContent) {
            // file deletion
            if ($dirContent->isFile() || $dirContent->isLink()) {
                unlink($dirContent->getPathName());
            } else {
                // recursive directory deletion
                if (!$dirContent->isDot() && $dirContent->isDir()) {
                    self::remove($dirContent->getPathName());
                }
            }
        }
        unset($dir);
        unset($dirContent);

        // removes the parent directory
        if ($deleteDir) {
            rmdir($path);
        }
    }
public static function remove($path, $deleteDir = true)
    {
        // minimum security check
        if ($path == '' || $path == '/' || $path == DIRECTORY_SEPARATOR) {
            throw new \InvalidArgumentException('The root cannot be removed !!');
        }

        if (!file_exists($path)) {
            return true;
        }

        $dir = new \DirectoryIterator($path);
        foreach ($dir as $dirContent) {
            // file deletion
            if ($dirContent->isFile() || $dirContent->isLink()) {
                unlink($dirContent->getPathName());
            } else {
                // recursive directory deletion
                if (!$dirContent->isDot() && $dirContent->isDir()) {
                    self::remove($dirContent->getPathName());
                }
            }
        }
        unset($dir);
        unset($dirContent);

        // removes the parent directory
        if ($deleteDir) {
            rmdir($path);
        }
    }
public static function remove ( $ path , $ deleteDir = true ) { // minimum security check if ( $ path == '' || $ path == '/' || $ path == DIRECTORY_SEPARATOR ) { throw new \ InvalidArgumentException ( 'The root cannot be removed !!' ) ; } if ( ! file_exists ( $ path ) ) { return true ; } $ dir = new \ DirectoryIterator ( $ path ) ; foreach ( $ dir as $ dirContent ) { // file deletion if ( $ dirContent -> isFile ( ) || $ dirContent -> isLink ( ) ) { unlink ( $ dirContent -> getPathName ( ) ) ; } else { // recursive directory deletion if ( ! $ dirContent -> isDot ( ) && $ dirContent -> isDir ( ) ) { self :: remove ( $ dirContent -> getPathName ( ) ) ; } } } unset ( $ dir ) ; unset ( $ dirContent ) ; // removes the parent directory if ( $ deleteDir ) { rmdir ( $ path ) ; } }
Recursive function deleting a directory.

@param string $path      The path of the directory to remove recursively
@param bool   $deleteDir If the path must be deleted too

@author Loic Mathaud
Recursive function deleting a directory .