note 86112 modified in function.rmdir by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
here is a little improved 'removing' function with DirectoryIterator:

note for PHP on windows: if using the iterator and recursively removing dirs and files, u need to unset() before leaving/continue the foreach! ( permission denied problem...)

<?php

function removeRessource( $_target ) {
    
    //file?
    if( is_file($_target) ) {
        if( is_writable($_target) ) {
            if( @unlink($_target) ) {
                return true;
            }
        }
        
        return false;
    }
    	
    //dir?
    if( is_dir($_target) ) {
        if( is_writeable($_target) ) {
            foreach( new DirectoryIterator($_target) as $_res ) {
                if( $_res->isDot() ) {
                    unset($_res);
                    continue;
                }
					
                if( $_res->isFile() ) {
                    removeRessource( $_res->getPathName() );
                } elseif( $_res->isDir() ) {
                    removeRessource( $_res->getRealPath() );
                }
                
                unset($_res);
            }
            		
            if( @rmdir($_target) ) {
                return true;
            }
        }
        
        return false;
    }
}
?

--was--
here is a little improved 'removing' function with DirectoryIterator:

note for PHP on windows: if using the iterator and recursively removing dirs and files, u need to unset() before leaving/continue the foreach! ( permission denied problem...)

<?php

function removeRessource( $_target ) {
    $_target = realpath($_target);
    
    //file?
    if( is_file($_target) ) {
        if( is_writable($_target) ) {
            if( @unlink($_target) ) {
                return true;
            }
        }
        
        return false;
    }
    	
    //dir?
    if( is_dir($_target) ) {
        if( is_writeable($_target) ) {
            foreach( new DirectoryIterator($_target) as $_res ) {
                if( $_res->isDot() ) {
                    unset($_res);
                    continue;
                }
					
                if( $_res->isFile() ) {
                    removeRessource( $_res->getPathName() );
                } elseif( $_res->isDir() ) {
                    removeRessource( $_res->getRealPath() );
                }
                
                unset($_res);
            }
            		
            if( @rmdir($_target) ) {
                return true;
            }
        }
        
        return false;
    }
}
?

http://php.net/manual/en/function.rmdir.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.