How to synchronize two folders in PHP?

I use windows, mysql db, php

I am creating a Joomla component, one of which is folder synchronization. I want to synchronize two folders with a different name. How can i do this? It must be on the same machine, it does not involve two different servers.

How to synchronize two folders in PHP?

UPDATED

In the context of Alex Reply

What if I edit any .jpg file in one folder and save it with the same name as him, this file is already present in another folder. And I want this edited pic to be moved to another folder.? I also have a structured Joomla file structured for comparison

UPDATE 2

I have a recursive function that displays me the full folder structure ... if you can just use it to solve concat ur in ...

<?
function getDirectory($path = '.', $ignore = '') {
    $dirTree = array ();
    $dirTreeTemp = array ();
    $ignore[] = '.';
    $ignore[] = '..';

    $dh = @opendir($path);

    while (false !== ($file = readdir($dh))) {

        if (!in_array($file, $ignore)) {
            if (!is_dir("$path/$file")) {
                $stat = stat("$path/$file");
                $statdir = stat("$path");
                $dirTree["$path"][] = $file. " === ". date('Y-m-d H:i:s', $stat['mtime']) . " Directory == ".$path."===". date('Y-m-d H:i:s', $statdir['mtime']) ;

            } else {

                $dirTreeTemp = getDirectory("$path/$file", $ignore);
                if (is_array($dirTreeTemp))$dirTree = array_merge($dirTree, $dirTreeTemp);
            }
        }
    }
    closedir($dh);

    return $dirTree;
}


$ignore = array('.htaccess', 'error_log', 'cgi-bin', 'php.ini', '.ftpquota');

$dirTree = getDirectory('.', $ignore);
?>
<pre>
    <?
    print_r($dirTree);
    ?>
</pre> 
+3
4

, , ,

function sync() {

    $files = array();
    $folders = func_get_args();

    if (empty($folders)) {
        return FALSE;
    }

    // Get all files
    foreach($folders as $key => $folder) {
        // Normalise folder strings to remove trailing slash
        $folders[$key] = rtrim($folder, DIRECTORY_SEPARATOR);   
        $files += glob($folder . DIRECTORY_SEPARATOR . '*');    
    }

    // Drop same files
    $uniqueFiles = array();
    foreach($files as $file) {
        $hash = md5_file($file);

        if ( ! in_array($hash, $uniqueFiles)) {
            $uniqueFiles[$file] = $hash; 
        }
    }


    // Copy all these unique files into every folder
    foreach($folders as $folder) {
        foreach($uniqueFiles as $file => $hash) {
                copy($file, $folder . DIRECTORY_SEPARATOR . basename($file));
        }
    }
    return TRUE;    
}

// usage

sync('sync', 'sync2');

, . , (.. ).

, , - . , . , filemtime().

, , . , : (

$source = '/path/to/source/';
$destination = 'path/to/destination/';

$sourceFiles = glob($source . '*');

foreach($sourceFiles as $file) {

    $baseFile = basename($file);

    if (file_exists($destination . $baseFile)) {

        $originalHash = md5_file($file);
        $destinationHash = md5_file($destination . $baseFile);
        if ($originalHash === $destinationHash) {
            continue;
        }

    }
    copy($file, $destination . $baseFile);
}

, . .

+5

, , , , .....

//sync the files and folders

function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755)) 
{ 
    //$result = false; 
    if (is_file($source)) { 

        if ($dest[strlen($dest)-1]=='/') { 
            if (!file_exists($dest)) { 
                cmfcDirectory::makeAll($dest,$options['folderPermission'],true); 
            } 
            $__dest=$dest."/".basename($source); 
        } else { 
            $__dest=$dest; 
        } 
        $result = copy($source, $__dest); 
        chmod($__dest,$options['filePermission']); 

    } elseif(is_dir($source)) { 
        if ($dest[strlen($dest)-1]=='/') { 
            if ($source[strlen($source)-1]=='/') { 
                //Copy only contents 
            } else { 
                //Change parent itself and its contents 
                $dest=$dest.basename($source); 
                @mkdir($dest); 
                chmod($dest,$options['filePermission']); 
            } 
        } else { 
            if ($source[strlen($source)-1]=='/') { 
                //Copy parent directory with new name and all its content 
                @mkdir($dest,$options['folderPermission']); 
                chmod($dest,$options['filePermission']); 
            } else { 
                //Copy parent directory with new name and all its content 
                @mkdir($dest,$options['folderPermission']); 
                chmod($dest,$options['filePermission']); 
            } 
        } 

        $dirHandle=opendir($source); 
        while($file=readdir($dirHandle)) 
        { 
            if($file!="." && $file!="..") 
            { 
                 if(!is_dir($source."/".$file)) { 
                    $__dest=$dest."/".$file; 
                } else { 
                    $__dest=$dest."/".$file; 
                } 
                echo "$source/$file ||| $__dest<br />"; 
                $result=smartCopy($source."/".$file, $__dest, $options); 
            } 
        } 
        closedir($dirHandle); 

    } else { 
        $result=false; 
    } 
    return $result; 
} 
//end of function

echo smartCopy('media', 'mobile/images');
+2
function recurse_copy($src,$dst) {
  $dir = opendir($src); 
  @mkdir($dst); 
  while(false !== ( $file = readdir($dir)) ) { 
      if (( $file != '.' ) && ( $file != '..' )) { 
         if ( is_dir($src . '/' . $file) ) {
             recurse_copy($src . '/' . $file,$dst . '/' . $file); 
         } 
         else {
             if(md5_file($src . '/' . $file) !== md5_file($dst . '/' . $file))
             {
                echo 'copying' . $src . '/' . $file; echo '<br/>';
                copy($src . '/' . $file, $dst . '/' . $file);
             }
         } 
      } 
  } 
 closedir($dir); 
}
+1

Linux/Posix ( , ), /:

$chk=`rsync -qrRlpt --delete $src $dest`;

.

0

Source: https://habr.com/ru/post/1744287/


All Articles