In this code I want to first download file from remote server to my system and then transfer that file to other remote server .This code not runs properly .Please help to correct thie code .
function ftp_transfer($conn_id_source, $conn_id_target, $list, $copymovedelete, $divelevel) {
// --------------
// This function copies/moves/deletes directories and files from an FTP server to the same
// or another FTP server. Files are first transferred from the source FTP server to the webserver,
// and then transferred to the target FTP server.
//
// $list[$i][“dirorfile”] contains d or - which indicates if its a directory or a file
// $list[$i][“dirfilename”] contains the entry name
// $list[$i][“sourcedirectory”] contains the source directory
// $list[$i][“targetdirectory”] contains the target directory
// $list[$i][“newname”] contains the new name if divelevel = 0; for deeper levels the newname is the entry name itself
// --------------
// -------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------
global $rms_globals, $rms_settings, $rms_result, $rms_output;
// -------------------------------------------------------------------------
// Initialization
// -------------------------------------------------------------------------
if ($divelevel == 0) {
$rms_output[“ftp_transfer”][] = “
- ”;
}
// Total number of directories and files on level 0 (chosen by the user)
$total_dirs_files = $list[“stats”][“directories”][“total_number”] + $list[“stats”][“files”][“total_number”];
// -------------------------------------------------------------------------
// For all directories
// -------------------------------------------------------------------------
for ($i=1; $i<=$list[“stats”][“directories”][“total_number”]; $i=$i+1) {
// Set the status
$message = __(“Processing entry %1$s”, javascriptEncode2($list[“directories”][$i][“dirfilename”])) . " ($i/$total_dirs_files)";
setStatus($i, $total_dirs_files, $message);
// Source and target
$source = glueDirectories($list[“directories”][$i][“sourcedirectory”], $list[“directories”][$i][“dirfilename”]);
if ($copymovedelete == “copy” || $copymovedelete == “move”) {
if ($divelevel > 0) { $target = glueDirectories($list[“directories”][$i][“targetdirectory”], $list[“directories”][$i][“dirfilename”]); } // Subdirectories keep their original names
else { $target = glueDirectories($list[“directories”][$i][“targetdirectory”], $list[“directories”][$i][“newname”]); } // First-level user-selected directories can have been renamed
}
else {
$target = “”;
}
// Print starting message
$rms_output[“ftp_transfer”][] = __(“Processing directory %1$s”, $source);
$rms_output[“ftp_transfer”][] = “
- ”;
// Check that the targetdirectory is not a subdirectory of the sourcedirectory
if (($conn_id_source == $conn_id_target) && ($copymovedelete != “delete”) && (isSubdirectory($source, $target) == true)) {
$rms_output[“ftp_transfer”][] = __(“The target directory %1$s is the same as or a subdirectory of the source directory %2$s, so this directory will be skipped”, $target, $source);
$rms_output[“ftp_transfer”][] = “
continue;
}
// Check if the directory contains a banned keyword
// If banned keyword - copy: continue
// If banned keyword - move: abort
if ($list[“directories”][$i][“selectable”] == “banned_keyword”) {
if ($copymovedelete == “copy”) {
$rms_output[“ftp_transfer”][] = __(“The directory %1$s contains a banned keyword, so this directory will be skipped”, $source);
$rms_output[“ftp_transfer”][] = “
continue;
}
elseif ($copymovedelete == “move”) {
$rms_output[“ftp_transfer”][] = __(“The directory %1$s contains a banned keyword, aborting the move”, $source);
$rms_output[“ftp_transfer”][] = “”;
return false;
}
}
// Create the targetdirectory
if ($copymovedelete == “copy” || $copymovedelete == “move”) {
$success1 = ftp_mkdir($conn_id_target, $target);
if ($success1 == false) { $rms_output[“ftp_transfer”][] = __(“Unable to create the subdirectory %1$s. It may already exist. Continuing the copy/move process…”, $target); }
else { $rms_output[“ftp_transfer”][] = __(“Created target subdirectory %1$s”, $target); }
}
// Get a new list
$newlist = ftp_getlist($conn_id_source, $source);
if ($rms_result[“success”] == false) {
$rms_output[“ftp_transfer”][] = __(“The directory %1$s could not be selected, so this directory will be skipped”, $source);
$rms_output[“ftp_transfer”][] = “”;
setErrorVars(true, “”, “”, “”, “”);
continue;
}
// Add information to the list
for ($j=1; $j<=$newlist[“stats”][“directories”][“total_number”]; $j++) {
$newlist[“directories”][$j][“sourcedirectory”] = $source;
$newlist[“directories”][$j][“targetdirectory”] = $target;
}
for ($j=1; $j<=$newlist[“stats”][“files”][“total_number”]; $j++) {
$newlist[“files”][$j][“sourcedirectory”] = $source;
$newlist[“files”][$j][“targetdirectory”] = $target;
}
// Call the function recursively
$newdivelevel = $divelevel + 1;
$ftp_copymovedelete_result = ftp_transfer($conn_id_source, $conn_id_target, $newlist, $copymovedelete, $newdivelevel);
// Delete the source directory
// (Only if there were no problems in the recursive call to ftp_transfer() above)
if ($ftp_copymovedelete_result == true && ($copymovedelete == “move” || $copymovedelete == “delete”)) {
ftp_rmdir2($conn_id_source, $source);
if ($rms_result[“success”] == false) {
setErrorVars(true, “”, “”, “”, “”);
$rms_output[“ftp_transfer”][] = __(“Unable to delete the subdirectory %1$s - it may not be empty”, $source);
}
else {
$rms_output[“ftp_transfer”][] = __(“Deleted subdirectory %1$s”, $source);
}
}
// Print ending message
$rms_output[“ftp_transfer”][] = __(“Processing of directory %1$s completed”, $source);
$rms_output[“ftp_transfer”][] = “”;
} // end for list_directories
// -------------------------------------------------------------------------
// Process the files
// -------------------------------------------------------------------------
for ($i=1; $i<=$list[“stats”][“files”][“total_number”]; $i=$i+1) {
// Set the status
$j = $list[“stats”][“directories”][“total_number”] + $i;
$message = __(“Processing entry %1$s”, javascriptEncode2($list[“files”][$i][“dirfilename”])) . " ($j/$total_dirs_files)";
setStatus($j, $total_dirs_files, $message);
// ------------------------------------
// Copy and move
// ------------------------------------
if ($copymovedelete == “copy” || $copymovedelete == “move”) {
// Source and target
$source = glueDirectories($list[“files”][$i][“sourcedirectory”], $list[“files”][$i][“dirfilename”]);
if (isset($list[“files”][$i][“newname”])) { $target = glueDirectories($list[“files”][$i][“targetdirectory”], $list[“files”][$i][“newname”]); }
else { $target = glueDirectories($list[“files”][$i][“targetdirectory”], $list[“files”][$i][“dirfilename”]); }
// Check that the target is not the same as the source file
if (($conn_id_source == $conn_id_target) && ($target == $source)) {
$rms_output[“ftp_transfer”][] = __(“The target for file %1$s is the same as the source, so this file will be skipped”, $source);
continue;
}
// Check if the file contains a banned keyword, and if it is not bigger than the limit
// If banned keyword or too big - copy: continue with the other files
// If banned keyword or too big - move: abort
if ($list[“files”][$i][“selectable”] == “banned_keyword”) {
if ($copymovedelete == “copy”) {
$rms_output[“ftp_transfer”][] = __(“The file %1$s contains a banned keyword, so this file will be skipped”, $source);
continue;
}
elseif ($copymovedelete == “move”) {
$rms_output[“ftp_transfer”][] = __(“The file %1$s contains a banned keyword, aborting the move”, $source);
return false;
}
}
elseif ($list[“files”][$i][“selectable”] == “too_big”) {
if ($copymovedelete == “copy”) {
$rms_output[“ftp_transfer”][] = __(“The file %1$s is too big to be copied, so this file will be skipped”, $source);
continue;
}
elseif ($copymovedelete == “move”) {
$rms_output[“ftp_transfer”][] = __(“The file %1$s is too big to be moved, aborting the move”, $source);
return false;
}
}
// Get file from remote sourcedirectory to local temp directory
// Don’t delete the source file yet
$localtargetdir = $rms_globals[“application_tempdir”];
$localtargetfile = $list[“files”][$i][“dirfilename”] . “.txt”;
$remotesourcedir = $list[“files”][$i][“sourcedirectory”];
$remotesourcefile = $list[“files”][$i][“dirfilename”];
$ftpmode = ftpAsciiBinary($list[“files”][$i][“dirfilename”]);
$copymove = “copy”;
ftp_getfile($conn_id_source, $localtargetdir, $localtargetfile, $remotesourcedir, $remotesourcefile, $ftpmode, $copymove);
if ($rms_result["success"] == false) {
setErrorVars(true, "", "", "", "");
if ($copymovedelete == "copy") {
$rms_output["ftp_transfer"][] = __("Unable to copy the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
continue;
}
elseif ($copymovedelete == "move") {
$rms_output["ftp_transfer"][] = __("Unable to move the file <b>%1\$s</b>, aborting the move", $list["files"][$i]["dirfilename"]);
return false;
}
}
// Put file from local temp directory to remote targetdirectory
// Delete the temporary file
$localsourcedir = $rms_globals[“application_tempdir”];
$localsourcefile = $list[“files”][$i][“dirfilename”] . “.txt”;
$remotetargetdir = $list[“files”][$i][“targetdirectory”];
if (isset($list[“files”][$i][“newname”])) { $remotetargetfile = $list[“files”][$i][“newname”]; }
else { $remotetargetfile = $list[“files”][$i][“dirfilename”]; }
$copymove = “move”;
ftp_putfile($conn_id_target, $localsourcedir, $localsourcefile, $remotetargetdir, $remotetargetfile, $ftpmode, $copymove);
if ($rms_result["success"] == false) {
setErrorVars(true, "", "", "", "");
if ($copymovedelete == "copy") {
$rms_output["ftp_transfer"][] = __("Unable to copy the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
continue;
}
elseif ($copymovedelete == "move") {
$rms_output["ftp_transfer"][] = __("Unable to move the file <b>%1\$s</b>, aborting the move", $list["files"][$i]["dirfilename"]);
return false;
}
}
// Copy: if the operation is successful, print a message
elseif ($rms_result[“success”] == true && $copymovedelete == “copy”) {
$rms_output[“ftp_transfer”][] = __(“Copied file %1$s”, $list[“files”][$i][“dirfilename”]);
}
// Move: only if the operation is successful, delete the source file
elseif ($copymovedelete == “move”) {
$remotesource = glueDirectories($list[“files”][$i][“sourcedirectory”], $list[“files”][$i][“dirfilename”]);
ftp_delete2($conn_id_source, $remotesource);
if ($rms_result["success"] == false) {
setErrorVars(true, "", "", "", "");
$rms_output["ftp_transfer"][] = __("Unable to move the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
}
else {
$rms_output["ftp_transfer"][] = __("Moved file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
}
}
} // end copy or move
// ------------------------------------
// Delete
// ------------------------------------
elseif ($copymovedelete == “delete”) {
$remotesource = glueDirectories($list[“files”][$i][“sourcedirectory”], $list[“files”][$i][“dirfilename”]);
ftp_delete2($conn_id_source, $remotesource);
if ($rms_result["success"] == false) {
setErrorVars(true, "", "", "", "");
$rms_output["ftp_transfer"][] = __("Unable to delete the file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
continue;
}
else {
$rms_output["ftp_transfer"][] = __("Deleted file <b>%1\$s</b>", $list["files"][$i]["dirfilename"]);
}
} // end delete
} // end for list_files
if ($divelevel == 0) { $rms_output["ftp_transfer"][] = "</ul>"; }
// Print message
if ($divelevel == 0) { $rms_output[“ftp_transfer”][] = __(“All the selected directories and files have been processed.”); }
return true;
} // End function ftp_transfer