Synchronisation de deux dossiers

Contenu du snippet

Permet de synchroniser deux dossier récursivement, ont peut choisir l'extention de copy et ont a le chois d'écraser ou de ne pas écraser les fichiers existants déja... rien de compliquer apffichage coloré...

Source / Exemple :


<?php
/**

  • Synchronisation dossiers.
  • @author dlaserre
*
  • /
Class synchro { private $destination; public $haveErrors = array(); public $source; // Optionnal parameters. private $type; /* Mutators */ public function _setDestination($destination) { echo 'Synchronisation destination : '.$destination.PHP_EOL; $this->destination = $destination; } public function _setSource($source) { echo 'Synchronisation source : '.$source.PHP_EOL; $this->source = $source; } public function _setType($type) { echo 'Synchronisation type : '.$type.PHP_EOL; $this->type = array($type); } /* End Mutators */ /**
  • Copy file in destination.
  • @param string $file
  • /
protected function _copy($dir, $file) { $path = substr($dir, $this->_checkPath($dir)); if(!file_exists($this->destination.$path.$file)) { if (copy($dir.$file, $this->destination.$path.$file)) return (true); } return (false); } /**
  • Return offset of difference path source and file.
  • @param unknown $path
  • @return offset of difference.
  • /
protected function _checkPath($path) { if(!empty($path)) { for($offset = 0; $offset < strlen($path); $offset++) { if($path[$offset] != ((!empty($this->source[$offset]))?$this->source[$offset]:null)) return ($offset); } } } /**
  • Create recursive directory for copy files
  • @param unknown $path
  • /
protected function createArbo($path) { $path = substr($path, $this->_checkPath($path)); if(!file_exists($this->destination.$path)) { mkdir($this->destination.$path, true); // get permitions of source file. system('chmod -R '.intval(substr(sprintf('%o', fileperms($this->source.$path)), -3)).' '.$this->destination.$path); } } /**
  • Erase file already exists on destination.
  • /
public function retryingFilesFailds() { foreach($this->haveErrors as $file) { $path_ = explode('|', $file); $path = substr($path_[0], strlen($this->source)); $file = str_replace('|', '/', $file); if(strlen($path) > 0) echo "\t"; if(copy($file, $this->destination.$path.$path_[1])) echo "\33[0;34m".$path."\033[37m".' '.$file." \033[32mErase complited\033[37m".PHP_EOL; else echo "\033[31m".$path."\033[37m".' '.$file." \033[32mErase faild\033[37m".PHP_EOL; } } /**
  • Lecture du directory
  • @param string $Directory
  • /
public function _dir($Directory = null, $i = 0, $indir = false) { if($Directory == NULL) $Directory = $this->source; $fd = opendir($Directory); while($entry = @readdir($fd)) { if($entry != '..' and $entry != '.' and $entry != '.svn') { if(is_dir($Directory.'/'.$entry)) { $this->createArbo($Directory.'/'.$entry); if($indir) { for($a = 0; $a < $i; $a++) echo "\t"; } echo "\33[0;34m".$Directory."/".$entry."\033[37m".PHP_EOL; $this->_dir($Directory.'/'.$entry, $i+1, true); } else { if($indir) { for($a = 1; $a < $i+1; $a++) echo "\t"; } if(!empty($this->type)) { $fil = explode('.', $entry); if(!in_array(@$fil[1], $this->type)) { echo '- '.$entry." \033[33mSkiped\033[37m".PHP_EOL; } else { echo '- '.$entry; if(self::_copy($Directory.'/', $entry)) echo " \033[32mOK\033[37m".PHP_EOL; else { echo " \033[31mExist\033[37m".PHP_EOL; $this->haveErrors[] = $Directory.'|'.$entry; } } } else { echo '- '.$entry; if(self::_copy($Directory.'/', $entry)) echo " \033[32mOK\033[37m".PHP_EOL; else { echo " \033[31mExist\033[37m".PHP_EOL; $this->haveErrors[] = $Directory.'|'.$entry; } } } } } if($fd) closedir($fd); } /**
  • Help function
  • toString
  • /
public function __toString() { $return = '------------------------------------------------------'.PHP_EOL; $return .= '- Usage : php synchro_img_mt.php'.PHP_EOL; $return .= '- Options :'.PHP_EOL; $return .= '- s : Source copying "file dont end path with /"'.PHP_EOL; $return .= '- d : Destination copying files "dont end path with /"'.PHP_EOL; $return .= '-- Optionnal --'.PHP_EOL; $return .= '- t : Type of file ex : .jpg, .ini, .php, etc...'.PHP_EOL; $return .= '------------------------------------------------------'.PHP_EOL; return ($return); } public function __destruct() {} } $options = array('d' => 'destination', 's' => 'source', 't' => 'type'); $syn = new synchro(); $arg = getopt('h:d:s:t:'); foreach($arg as $option => $value) { if(!array_key_exists('h', $arg)) { if(array_key_exists($option, $options)) { call_user_func(array($syn,'_set'.ucfirst($options[$option])), $value); } } else { die($syn); } } $syn->_dir(); if(!empty($syn->haveErrors)) { $handle = fopen("php://stdin", 'r'); echo count($syn->haveErrors).' Exists ! erase exists ? [y/n] : '; $responce = fgets($handle); if( $responce == "y\n") { $syn->retryingFilesFailds(); } } ?>

Conclusion :


a votre bon coeur !

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.