PHP iki klasörleri senkronize etmek için nasıl?

3 Cevap php

Ben windows kullanıyorum, MySQL DB, PHP

I am building the Joomla Component whose one of the functionality is to synchronize the folders I want to sync two folders of different name.. How can I do this? It has to be dne in the same machine, no two different servers are involved in it..

PHP iki klasörleri senkronize etmek için nasıl?

UPDATED

Alex Cevap Bağlamında IN

What if i am editing any .jpg file in one folder and saving it with same name as it has, also this file is already present in other folder. And I want this edited pic to be transfered to the other folder.? Ayrıca ben karşılaştırılmak üzere yapılandırılmış Joomla'nın iç içe dosyası var

UPDATE 2

Ben u sadece içeri ur çözüm CONCAT için kullanabilirsiniz eğer çıktısı bana klasörün tam yapısı ... tekrarlamalı bir fonksiyon var ..

<?
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 Cevap

Ben sadece bu koştum, ve o iş gibi görünüyor

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');

Siz sadece bunu eşitlemek için klasörlerin bir listesini vermek, ve tüm dosyaları senkronize edecektir. Bu appear aynı (yani onların karma çarpıştığı kime) dosyaları atlamak için çalışacaktır.

Bu ancak dikkate son değiştirilme tarihleri ​​ya da bir şey almaz. Bunu yapmak için kendisini değiştirmek zorunda kalacak. Bu filemtime() içine bakmak, oldukça basit olmalıdır.

Ayrıca, üzgünüm kod emerse. Ben özyinelemeli yapma gitmek vardı, ama başarısız oldu: (

Tek yönlü bir kopyası için, bu deneyin

$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);
}

Tek bir klasörden diğerine tüm dosyaları kopyalamak istediğiniz gibi geliyor. İkinci örnek yapacağız.

Yok eğer ben bu kullanıyorum ve o iş gibi görünüyor, aynı zamanda yeni dir olun .....

//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');

: Onun bir Linux / Posix'e sistemde daha sonra (ne var erişim bağlı olarak) o kadar basit / hızlı bir yeri olabilir, eğer

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

C.