if ($argc != 5) die("Invalid number of parameters! ($argc)"); $local_dir = $argv[1]; $ftp_host = $argv[2]; $ftp_user = $argv[3]; $ftp_pass = $argv[4]; $delay = 600; # delay for putting advertisement... echo "Building the list of local files.\r\n"; $dirlist[] = $local_dir; while ($dir = array_shift($dirlist)) { $d = opendir($dir); while ($file = readdir($d)) { if ($file != '.' && $file != '..') { $file = $dir . '/' . $file; if (is_dir($file)) $dirlist[] = $file; else { $tm = filemtime($file); $local_files[$file] = $tm; } } } closedir($d); } ksort($local_files); $conn = ftp_connect($ftp_host); if (!$conn) { die("Connection to $ftp_host failed."); } if (!ftp_login($conn, $ftp_user, $ftp_pass)) { die("Login for $ftp_user failed."); } echo "Building the list of remote files.\r\n"; $fdirs[] = '.'; while ($fd = array_shift($fdirs)) { unset($nlist); $nlist = ftp_nlist($conn, $fd); if ($nlist && is_array($nlist)) { foreach($nlist as $v) { $mt = ftp_mdtm($conn, $v); if ($mt < 0) { if (!preg_match('/\.$/', $v)) $fdirs[] = $remote_dirs[] = $v; } else { $remote_files[$v] = $mt; } } } } ksort($remote_files); # Removing the old files from the ftp server echo "\r\nRemoving the old files from the ftp server\r\n"; echo "==========================================\r\n"; foreach(array_keys($remote_files) as $rf) { $lf = $local_dir . '/' . $rf; if (!$local_files[$lf]) { echo "Deleting $rf\r\n"; ftp_delete($conn, $rf); } } reset($remote_files); # Uploading the new or changed files echo "\r\nUploading the new or changed files\r\n"; echo "==================================\r\n"; $llen = strlen($local_dir) + 1; # remote files do not have leading slashes $uploaded = 0; while (list($lf, $lt) = each($local_files)) { $rf = substr($lf, $llen); $rt = $remote_files[$rf]; $lsz = filesize($lf); # size of the local file if ($rt) { # file exists $rsz = ftp_size($conn, $rf); # size of the remote file if ($rt - $delay <= $lt || $lsz > $rsz) { # the file is old or short for ($attempt = 0; $attempt < 10 && (!$attempt || $lsz > $rsz); $attempt++) { echo "Uploading $rf\r\n"; echo "$rf $rsz $lsz\r\n"; if (!ftp_put($conn, $rf, $lf, FTP_BINARY)) { ftp_quit($conn); die('File upload failed.'); } $rsz = ftp_size($conn, $rf); # size of the remote file } $uploaded++; } } else { # new file makedir4($rf); $rsz = 0; for ($attempt = 0; $attempt < 10 && $lsz > $rsz; $attempt++) { echo "Uploading $rf\r\n"; echo "$rf $rsz $lsz\r\n"; if (!ftp_put($conn, $rf, $lf, FTP_BINARY)) { ftp_quit($conn); die('File upload failed.'); } $rsz = ftp_size($conn, $rf); # size of the remote file } $uploaded++; } } echo "$uploaded files uploaded.\r\n"; ftp_quit($conn); ?>