views:

91

answers:

2

I am using windows, 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..

How to sync two folders in PHP?

UPDATED

IN Context of Alex Reply

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.? also I have Joomla's nested file structured to be compared

UPDATE 2

I have a recursive function which will output me the complete structure of folder... if u can just use it to concat ur solution 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  A: 

I just ran this, and it seems to work

error_reporting(E_ALL);

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

You simply give it a list of folders to sync, and it will sync all files. It will attempt to skip files that appear the same (i.e. of whom their hash collides).

This however does not take into account last modified dates or anything. You will have to modify itself to do that. It should be pretty simple, look into filemtime().

Also, sorry if the code sucks. I had a go at making it recursive, but I failed :(

Update

For a one way copy, try this

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

It sounds like you just want to copy all files from one folder to another. The second example will do this.

alex
that's a nice piece of code.
Bingy
@Alex pls chk the Updated question as per ur reply...
OM The Eternity
@OM Am working on a better version as we speak
alex
also I have Joomla's nested file structured to be compared (both folders)
OM The Eternity
@alex can u just comment on ur edited answer what will it do?
OM The Eternity
You can compare last modified dates. Comparing md5 for megabytes of data could be overwhelming for your server.
Salman A
@OM I just added a better version. It will be slow if it's traversing many / large files. You can remove the mdf5 check to help.
alex
@ Alex Notic This.. "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.?"
OM The Eternity
@OM The second example will do that. It will overwrite files in the destination, but will leave them alone if they are the same.
alex
@bingy Thanks :) Hope it is useful too!
alex
@alex Chk my Update 2 part in question, I have a recursive function which will output me the complete structure of folder
OM The Eternity
@OM Not to be rude, but have you had a go yourself... ?
alex
@alex with ur recent code, no, but as u tried with recursion I had a recursion function, so thought if it could help u to get me the exact solution what i need.. we are very much close to what we need.. Please help...
OM The Eternity
@alex its not working ur sync script is not working for me, what should be the parameters to be passed in it? pls tell me....
OM The Eternity
You should pass in any number of paths which will be synced.
alex
A: 

If its on a Linux/Posix system then (depending on what access you have) it may be a lot simpler / faster to:

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

C.

symcbean
I wish If could have only these systems in the whole world.. No windows No Doors... :) But alaas, I have windows...
OM The Eternity