PHP Classes

File: DirectoryHandler

Recommend this page to a friend!
  Classes of Gilberto Albino   Directory Handler   DirectoryHandler   Download  
File: DirectoryHandler
Role: Class source
Content type: text/plain
Description: Class used to perform the directories operations
Class: Directory Handler
Rename, copy or delete directories
Author: By
Last change:
Date: 14 years ago
Size: 2,272 bytes
 

Contents

Class file image Download
<?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 contato@gilbertoalbino.com
 * 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 );
        }
    }
}