function form_remote_action()
{
global $path;
$path="/home/site.ru/www/";
// set up variables - change these to suit application
if(isset($_REQUEST['host']) && !empty($_REQUEST['host'])) $host = $_REQUEST['host']; else {echo ("HOSTNAME wasn't defined"); exit;}
if(isset($_REQUEST['login']) && !empty($_REQUEST['login'])) $login = $_REQUEST['login']; else {echo ("LOGIN wasn't defined"); exit;}
if(isset($_REQUEST['pass']) && !empty($_REQUEST['pass'])) $pass = $_REQUEST['pass']; else {echo ("PASSWORD wasn't defined"); exit;}
if($_REQUEST['mode']=="FTP_BINARY") $mode=FTP_BINARY; else $mode=FTP_ASCII;
if(isset($_REQUEST['folder_loc']) && !empty($_REQUEST['folder_loc'])) $folder_loc=$_REQUEST['folder_loc']; else {echo ("LOCARDIR wasn't defined"); exit;}
if(isset($_REQUEST['folder_rem']) && !empty($_REQUEST['folder_rem'])) $folder_rem=$_REQUEST['folder_rem']; else {echo ("REMOTEDIR wasn't defined"); exit;}
if(substr($folder_rem,0,1)=="/") $folder_rem=substr($folder_rem,1);
if(substr($folder_rem,-1)=="/") $folder_rem=substr($folder_rem,0,-1);
$remotedir_array=explode("/", $folder_rem);
$createdir=array_pop($remotedir_array);
if(substr($folder_loc,0,1)=="/") $folder_loc=substr($folder_loc,1);
if(substr($folder_loc,-1)=="/") $folder_loc=substr($folder_loc,0,-1);
$localdir = $path.$folder_loc;
if(!is_dir($localdir)) {echo ("LOCALDIR not existed <b>".$localdir."</b>"); exit;}
$met_old=ini_get('max_execution_time'); echo $met_old."<br>";
ini_set('max_execution_time', 90);
$met_new=ini_get('max_execution_time'); echo $met_new."<br>";
// connect to host
$conn = ftp_connect("".$host."");
if (!$conn)
{
echo "Error: Could not connect to ftp server ".$host."<br>";
ini_set('max_execution_time', $met_old);
exit;
}
echo "Connected to ".$host."<br>";
// log in to host
$result = ftp_login($conn, $login, $pass);
if (!$result)
{
echo "Error: Could not log on as ".$login."<br>";
ftp_quit($conn);
ini_set('max_execution_time', $met_old);
exit;
}
echo "Logged in as ".$login."<br>";
// show current directory
echo "Current directory: ".ftp_pwd($conn)."<br>";
// try to change the directory to somedir
foreach($remotedir_array as $new_dir)
{
if (ftp_chdir($conn, $new_dir))
{
echo "Current directory is now: ".ftp_pwd($conn)."<br>";
}
else
{
echo "Couldn't change directory<br>";
ftp_quit($conn);
ini_set('max_execution_time', $met_old);
exit;
}
}
// turn passive mode on
ftp_pasv($conn, true);
// try to create the remote directory
if (@ftp_mkdir($conn, $createdir))
{
echo "Successfully created ".$createdir."!<br>";
// try to change the directory to TARGETDIR
if (ftp_chdir($conn, $createdir))
{
echo "Current directory is now: ".ftp_pwd($conn)."<br>";
}
else
{
echo "Couldn't change directory<br>";
ftp_die();
}
}
else
{
echo "Directory ".$createdir." already exists!<br>";
ftp_die();
}
remote_recursive($localdir, $conn);
// close connection to host
echo "<br><font size='+1' color='red'>Operation successfully complete!</font><br>";
ftp_quit($conn);
ini_set('max_execution_time', $met_old);
return;
}
function remote_recursive($dir, $conn)
{
$dh=opendir($dir);
while($file=readdir($dh))
{
if($file!="." && $file!="..")
{
$localfile=$dir."/".$file;
if(is_dir($localfile))
{
if (@ftp_mkdir($conn, $file))
{
echo "Successfully created directory ".$file."!<br>";
// try to change the directory
if (ftp_chdir($conn, $file))
{
echo "Current directory is now: ".ftp_pwd($conn)."<br>";
}
else
{
echo "Couldn't change directory!<br>";
ftp_die();
}
}
else
{
echo "Can't create directory ".$file."!<br>";
ftp_die();
}
remote_recursive($localfile, $conn);
}
else
{
// upload file
echo "Upload file on remote server...<br>";
// Initiate the Upload
$ret = ftp_nb_put($conn, $file, $localfile, $mode);
while ($ret == FTP_MOREDATA)
{
// Do whatever you want
echo "...<br>";
// Continue uploading...
$ret = ftp_nb_continue($conn);
}
if ($ret != FTP_FINISHED)
{
echo "There was an error uploading the file...";
ftp_die();
}
echo "File downloaded successfully!<br>";
}
}
}
// Try change to parent directory
if(ftp_cdup($conn)) {echo "Change to parent directory successful!<br>";} else {echo "Failed change to parent directory!<br>"; ftp_die();}
closedir($dh);
}
function ftp_die($conn, $error, $met_old)
{
echo $error."<br>";
ftp_quit($conn);
ini_set('max_execution_time', $met_old);
exit;
}