使用Php从FTP获取文件

时间:2014-04-05 16:01:52

标签: php ftp

我正在尝试使用php中的ftp从另一台服务器将文件提取到我的服务器中。

有两个步骤。第一步是从我的服务器发送文件,我已成功完成。

现在我需要从仓库ftp服务器获取文件。我成功连接到它,并从那里获取文件列表。但我不知道如何才能获取它。这里发送的是代码。我尝试在MY SERVER和The Warehouse服务器之间切换,但它不起作用并继续说文件不存在,因为文件在那里。

有什么想法吗?

以下是发送文件的代码

$ftp_server    = SERVER_IP;
$ftp_user_name = FTP_USER;
$ftp_user_pass = FTP_PASS;
$ftp_dir       = "public_html/inbound/";
$mode          = "list_xml_files";
$file          = "";

if ($file_list = ftp_list_xml_files ($ftp_server,$ftp_user_name,$ftp_user_pass,$ftp_dir,$mode,$file)) {

$ftp_server    = WAREHOUE_SERVER;
$ftp_user_name = WAREHOUSE_FTP_USER;
$ftp_user_pass = WAREHOUSE_FTP_PASS;
$ftp_dir       = FILE_DIRECTORY;
$mode          = "ftp_to_warehouse";

foreach ($file_list as $v) {

if ($file_uploaded = ftp_list_xml_files ($ftp_server,$ftp_user_name,$ftp_user_pass,$ftp_dir,$mode,$v)) {

echo "FILE SENT";

}

else
{

echo "ERROR";

}


function ftp_list_xml_files ($ftp_server,$ftp_user_name,$ftp_user_pass,$ftp_dir,$mode,$file) {

$conn_id = ftp_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!<br>";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name<br>"; 
        exit; 
    } else {
        #echo "Connected to $ftp_server, for user $ftp_user_name";
    }

switch ($mode) {
    case "list_xml_files":
        $contents = array();
        ftp_chdir($conn_id, $ftp_dir); // Change Directory
        // get .xml files in the current directory
        $contents = ftp_nlist($conn_id, "*.xml");
        /////////////////////////////////////////////////////////////// What if there's no files to process?
        if (count($contents) > 0) {
            // output $contents
            // print_r($contents);
            // exit;
            return $contents;
        } else {
            return FALSE;
        }           
        break;
case "ftp_to_warehouse":
        // upload the file
        $source_file = 'tracking_number_xmls/pending/' . $file;

        ftp_chdir($conn_id, $ftp_dir); // Change Directory
        $destination_file = $file;

        $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_TEXT); // FTP_BINARY

        // check upload status
        if (!$upload) { 
            echo "FTP upload has failed!<br>";
            return FALSE;
        } else {
            echo "Uploaded $source_file to $ftp_server as $destination_file<br>";
            return TRUE;
        }
        break;

default:
        echo "Nothing Done<br>";
}

// close the FTP stream 
ftp_close($conn_id);
} // end function

0 个答案:

没有答案
相关问题