Location: PHPKode > scripts > 3SS Sync File > 3ss-sync-file/threesssyncfile.class.php
<?php
class threessSyncFile
{
    ///////////////////////////////////////////////
    //
    //    3 Service Solution
    //    http://www.3servicesolution.com/
    //    Author: Brant Messenger
    //
    //    COPYRIGHT:
    //    http://creativecommons.org/licenses/by-sa/2.0/
    //
    //    LOCATION:
    //    http://www.3servicesolution.com/_classes/threesssyncfile.class.inc
    //
    //    VERSION: 1.1
    //
    ///////////////////////////////////////////////
    
    var $remote;
    var $local;
    var $handle;
    var $str;
    var $ext;
    
    
    
    function threessSyncFile($remote,$local)
    {
        $this->oFile = $remote;
        $this->cFile = $local;
        $this->debug = False;
        $this->equal = True;
   
    }
        
    function writeError($str)
    {
        if ($this->debug == True)
        {
        echo '
        <div style="color:#c00">'.$str.'</div>';
        exit;
        }

    }
    
    function write($str)
    {
        echo '
        <div style="color:#c00">'.$str.'</div>';

    }    
    
    function getSource()
    {
        if (function_exists("file_get_contents"))
        {
            if (!$this->oFileStr = @file_get_contents($this->oFile)) $this->writeError("file_get_contents [ $this->oFile ] Could not get remote Source");
            if (!$this->cFileStr = @file_get_contents($this->cFile)) $this->cFileStr = "";

        }
        else
        {
            if (!$handle = fopen($this->oFile, "r")) $this->writeError("FOPEN [ $this->oFile ] Could not get remote Source");
            $this->oFileStr = fgets($handle, filesize($this->oFile));
            fclose($handle);
            
            if (!$handle = fopen($this->cFile, "r")) $this->writeError("FOPEN [ $this->cFile ] Could not get Copy Source");
            $this->cFileStr = fgets($handle, filesize($this->cFile));
            fclose($handle);           
        
        }

        return true;
    }
    
    function compareSource()
    {
        if ($this->oFileStr !== $this->cFileStr)
        {
            $this->equal = False;
            return true;
        }
        else return false;
        
    }
    
    function writeSource()
    {

        if ($this->cFileStr != "" && $this->equal == False)
        {        
            if (!@copy ($this->cFile, $this->cFile.".".date("Ymdhis"))) $this->writeError(" could not be copied");
      
            if (!is_writable($this->cFile)) $this->writeError(" is not writable");
            
        }
        
        if (function_exists("file_put_contents"))
        {
            if (!file_put_contents ($this->oFileStr,$this->cFile)) $this->writeError("file_put_contents [ $this->cFile ] could not write");
        }
        else
        {         
            if (!$handle = fopen($this->cFile, 'w')) $this->writeError("FOPEN [ $this->cFile ] could not open handle");
        
            if (!fwrite($handle, $this->oFileStr)) $this->writeError("FWRITE [ $this->cFile ] could not write");
                
            fclose($handle);
        }
        return true;

    }
    
    function startSync()
    {
        if (!$this->getSource()) $this->writeError("startSync [ getSource ] FALSE");
        if (!$this->compareSource()) $this->writeError("startSync [ compareSource ] FALSE");
        if (!$this->writeSource()) $this->writeError("startSync [ writeSource ] FALSE");
        else $this->write("LOCAL [ $this->cFile ] has by synchronized");

    }
}
?>
Return current item: 3SS Sync File