{
$errormessage = '';
$connectionid = getftpconnection($uploadlocation['host'], $uploadlocation['username'], $uploadlocation['password'], $uploadlocation['port']);
switch($ftptype)
{
case 'active':
ftp_pasv($connectionid, false);
break;
case 'passive':
ftp_pasv($connectionid, true);
break;
}
$basedirectory = $uploadlocation['basedirectory'];
if(substr($basedirectory, strlen($basedirectory) - 1, 1) != '/')
{
$basedirectory .= '/';
}
ftp_mkdir($connectionid, $basedirectory); // no point showing an error message if the directory exists (most likely cause of error) because it will exist (at least) after the first time.
$remotebasedirectory = $basedirectory.$remotedirectory;
if(substr($remotebasedirectory, strlen($remotebasedirectory) - 1, 1) == '/')
{
$remotebasedirectory = substr($remotebasedirectory, 0, strlen($remotebasedirectory) - 1);
}
$remotebasedirectory .= '/';
$errormessage .= copyfileviaftp($previewpath, $remotebasedirectory, $connectionid);
ftp_close($connectionid);
$errorhtml = '';
if($errormessage)
{
$errorhtml = nl2br($errormessage);
}
return $errorhtml;
}
function getftpconnection($host, $username, $password, $port)
{
$connectionid = ftp_connect($host);
if(!@ftp_login($connectionid, $username, $password))
{
webserviceerror('ftp error. unable to connect to '.$host.' with username '.$username.'');
}
return $connectionid;
}
function copyfileviaftp($sourcepath, $destinationpath, $connectionid)
{
$errormessage = '';
$sourcepath = str_replace( , -, $sourcepath);
$destinationpath = str_replace( , -, $destinationpath);
if(!ftp_mkdir($connectionid, $destinationpath))
{
$errormessage .= unable to create directory at .$destinationpath. (it may already exist) ;
}
ftp_site($connectionid, 'chmod 0777 '.$destinationpath);
ftp_chdir($connectionid, $destinationpath);
//print $sourcepath.' to '.$destinationpath.
;
if(is_dir($sourcepath))
{
chdir($sourcepath);
$handle=opendir('.');
while(($file = readdir($handle))!==false)
{
if(($file != .) && ($file != ..))
{
if(is_dir($file))
{
$errormessage .= copyfileviaftp($sourcepath.directory_separator.$file, $file, $connectionid);
chdir($sourcepath);
if(!ftp_cdup($connectionid))
{
$errormessage .= unable to ftp_cdup. ;
}
}
else
{
if(substr($file, strlen($file) - 4, 4) != .zip)
{
$fp = fopen($file,r);
if(!ftp_fput($connectionid, str_replace( , _, $file), $fp, ftp_binary))
{
$errormessage .= unable to ftp_fput(). ;
}
ftp_site($connectionid, 'chmod 0755 '.str_replace( , _, $file));
}
}
}
}
closedir($handle);
}
return $errormessage;
}
