<?php
/*
Uploading files to the WWW server via FTP, version 1.1
(C) Z. Wagner -- Ice Bear Soft, 2001, 2004, 2006
http://icebearsoft.euweb.cz
This PHP script is free software. Its use and distribution should follow
the General Public Licence (see http://www.gnu.org). Since this licence
is void in the Czech Republic, the users may opt to use a modified version
available from http://www.zastudena.cz
You can find more information on http://icebearsoft.euweb.cz/czgpl/
*/
# Make directory for item
function makedir4($item) {
global $remote_dirs, $conn;
if (preg_match('/^(.+)\/(.+)\/(.+)$/', $item, $m)) {
$d = $m[1] . '/' . $m[2];
if (!in_array($d, $remote_dirs)) {
if (!in_array($m[1], $remote_dirs)) makedir4($d);
echo "\r\nTrying to create directory $d\r\n";
if (!ftp_mkdir($conn, $d)) {
ftp_quit($conn);
die("Directory (1) $d cannot be created.");
}
$remote_dirs[] = $d;
}
}
elseif (preg_match('/^(.+)\/(.+)$/', $item, $m)) {
$d = $m[1];
if (!in_array($d, $remote_dirs)) {
echo "\r\nTrying to create directory $d\r\n";
if (!ftp_mkdir($conn, $d)) {
ftp_quit($conn);
die("Directory (2) $d cannot be created.");
}
$remote_dirs[] = $d;
}
}}
# Main program -- requires <local_dir> <ftp_host> <ftp_user> <ftp_password>
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);
?>