使用php将文件从服务器传输到另一台服务器中的文件夹

时间:2016-07-13 05:43:01

标签: php

这就是我想要的,将文件“change.php”放入客户端服务器(clientserver.com/testfolder)上的文件夹中。我能够将文件“change.php”从一台服务器移动到另一台服务器(myserver.com到clientserver.com)。这是我尝试但不能得到我想要的结果

<?php
/* Source File Name and Path */
    $remote_file = 'change.php';

/* FTP Account */
    $ftp_host = 'clientserver.com'; /* host */
    $ftp_user_name = 'username@clientserver.com'; /* username */
    $ftp_user_pass = 'password'; /* password */

/* Connect using basic FTP */
    $connect_it = ftp_connect( $ftp_host );

/* Login to FTP */
    $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );

/* New file name and path for this file */
    $dst_dir = 'testfolder/';
    $local_file = 'testfolder/change.php';

// turn passive mode on
    ftp_pasv($connect_it, true);

// try to creat path
    ftp_chdir($connect_it, $dst_dir);

/* Download $remote_file and save to $local_file */
    if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ){
        echo "WOOT! Successfully written to $local_file\n";
    } else {
        echo "Doh! There was a problem\n";
    }

/* Close the connection */
    ftp_close( $connect_it );

&GT;

2 个答案:

答案 0 :(得分:0)

ftp_chdir($connect_it, '$dst_dir');更改为:

ftp_chdir($connect_it, $dst_dir);

问题是PHP不会替换单引号字符串中的变量。

$var = 'BUH';
echo 'Foo $var Bar';
echo "Foo $bar Bar";

将导致此输出:

Foo $var Bar
Foo BUH Bar

答案 1 :(得分:0)

更改此

ftp_chdir($connect_it, '$dst_dir'); to ftp_chdir($connect_it, $dst_dir);