File Synchronization between a remote server and local windows computer
File Synchronization between a remote server and local windows computer
<?php
error_reporting(E_ALL & ~E_NOTICE);
require_once "functions.php";
$remoteService = "http://www.mywebsite.com/ wsdl/dirSyncRemote.php";
$remoteUrl = "http://www.mywebsite.com/ myfiles2sync/";
$localDir = "Z:\myfiles2sync\"; // This can be a shared folder
echo dirSync($remoteUrl, $localDir, $remoteService);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function dirSync($remoteUrl, $localDir, $remoteService){
$key = base64_encode(1.673*(int)date( "dh")); // Security key generator
$remoteService = "?key=$key";
$fileList = unserialize(base64_decode( file_get_contents($ remoteService)));
if(is_array($fileList)){
foreach($fileList as $key=>$file){
if (@copy($remoteUrl . $file, $_SERVER["DOCUMENT_ROOT"] . $localDir . $file))
{
$res .= "Remote file '' is copied to local dir '$localDir' ";
$res .= base64_decode(file_get_ contents($remoteService . "&bkp=" . $file));
}
}
}else{
echo "No files to Sync!";
}
return $res;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
dirSyncLocal.php [this file should be stored in the remote server]
<?php
// File directory to transfer & backup
$dir2Sync = "/myfiles2sync/";
$dir2SyncBkp = "/myfiles2sync/backup/";
if(($_GET["key"]) == base64_encode(1.673*(int)date( "Ymd"))){ // Check security key
if(!$_GET[bkp]){
// Serializing files list to be transfered
$directory = $_SERVER["DOCUMENT_ROOT"] . $dir2Sync;
$myDirectory = opendir($directory);
while($entryName = readdir($myDirectory)) {
if ( $entryName<>'.' AND $entryName<>'..' AND $entryName<>'filelist.txt' AND $entryName<>'backup' AND $entryName<>'.DS_Store'){
$res[] = $entryName;
}
}
closedir($myDirectory);
if(is_array($res)){
echo base64_encode(serialize($res)) ;
}
}else{
$filename = $_SERVER["DOCUMENT_ROOT"] . $dir2Sync . $_GET[bkp];
$new_filename = $_SERVER["DOCUMENT_ROOT"] . $dir2SyncBkp . $_GET[bkp];
if(file_exists($filename)){
rename($filename, $new_filename);
echo base64_encode("Remote file '' has been tranfered to backup folder. ");
}
}
}else{
echo "Invalid call!";
}
?>
Go Back