<?php
 
/**
 
 * Use this class if you want to perform directories operations like 'copy, delete and rename'
 
 * 
 
 * author        Gilberto Albino
 
 * version        1.3
 
 * license        No licenses aplied
 
 * contatct        [email protected]
 
 * url            http://www.gilbertoalbino.com * 
 
 * usage        See index.php for more information
 
 */
 
class DirectoryHandler
 
{    
 
    public function renameDirectory( $startDir, $endDir ) 
 
    {
 
        $this->copyDirectory( $startDir, $endDir );
 
        $this->deleteDirectory( $startDir );
 
    }
 
    
 
    public function copyDirectory( $startDir, $endDir ) 
 
    {
 
        if( is_dir($startDir) ) {
 
            if( !is_dir($endDir) ) {
 
                mkdir( $endDir );
 
            }
 
            for ( 
 
                $source = new DirectoryIterator($startDir);
 
                  $source->valid();
 
                  $source->next()
 
            ) {          
 
                if( $source->getFilename() == '.' || $source->getFilename() == '..' ) {
 
                    continue;
 
                } else {
 
                    if( $source->getType()== 'dir' ) {
 
                        mkdir( $endDir.DIRECTORY_SEPARATOR.$source->getFilename() );
 
                        $this->copyDirectory( $startDir.DIRECTORY_SEPARATOR.$source->getFilename(), $endDir.DIRECTORY_SEPARATOR.$source->getFilename() );                    
 
                    } else {
 
                        $content = @file_get_contents( $startDir.DIRECTORY_SEPARATOR.$source->getFilename() );                    
 
                        $openedfile = fopen( $endDir.DIRECTORY_SEPARATOR.$source->getFilename(), "w" );                    
 
                        fwrite( $openedfile, $content );
 
                        fclose( $openedfile );                    
 
                    }                 
 
                }
 
            }
 
            
 
        }    
 
    }
 
    
 
    
 
    public function deleteDirectory( $target ) 
 
    {
 
        if( is_dir( $target ) ) {
 
            chmod( $target, 0777 );
 
            for ( 
 
                $source = new DirectoryIterator( $target );
 
                  $source->valid();
 
                  $source->next()
 
            ) {     
 
                if( $source->getFilename() == '.' || $source->getFilename() == '..' ) {
 
                    continue;
 
                } else {            
 
                    if( $source->getType()== 'dir' ) {                                    
 
                        $this->deleteDirectory( $target.DIRECTORY_SEPARATOR.$source->getFilename() );
 
                        if( is_dir( $target.DIRECTORY_SEPARATOR.$source->getFilename() ) ) {                        
 
                            rmdir( $target.DIRECTORY_SEPARATOR.$source->getFilename() );
 
                        }                    
 
                    } else {                                    
 
                        unlink( $target.DIRECTORY_SEPARATOR.$source->getFilename() );
 
                    }
 
                }     
 
            }    
 
            rmdir( $target );    
 
        } 
 
    }    
 
}
 
 
 
 |