简体   繁体   English

使用php无法正常将文件从一台服务器上传到另一台服务器?

[英]Upload files from one server to another server using php not working?

I have tried in many ways to upload files using FTP functions in PHP. 我已经尝试过多种方式使用PHP中的FTP函数上传文件。 But it is not working for me. 但这对我不起作用。 I don't know what mistake I made in this code. 我不知道我在这段代码中犯了什么错误。 I have given the local file path or another server file path also, but in both cases it is not working. 我也给了本地文件路径或其他服务器文件路径,但在这两种情况下它都不起作用。 I have given my code below. 我在下面给出了我的代码。 Can anyone help to find my problem? 任何人都可以帮助找到我的问题?

/* Source File Name and Path */
$backup_file = '/var/www/html/artbak/assets/uploads/edi/test.txt';
//$backup_file = '/workspace/all-projects/artbak/assets/uploads/edi/test.txt';
$remote_file = $backup_file;

$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */ 

/* New file name and path for this file */
$local_file = '/public_html/example.txt';

/* 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 );

/* Download $remote_file and save to $local_file */
if ( ftp_put( $connect_it, $local_file, $remote_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 );

The below code is working for local to server upload, but not working for server to server upload. 以下代码适用于本地到服务器上载,但不适用于服务器到服务器上载。 It shows the server not connecting. 它显示服务器未连接。 Please give some ideas guys. 请给一些想法的人。 I am struggling in more in this concept. 我在这个概念中更加努力。

$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */ 

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

$file = "dummy.txt";

$remote_file = "receiveDummy.txt";

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

Thanks for your support guys. 谢谢你们的支持。 I am happy to post this answer couple of days work. 我很高兴发布这个答案几天的工作。 Only the thing is need to avoid the destination server path /public_html/test.txt instead of test.txt . 只需要避免使用目标服务器路径/public_html/test.txt而不是test.txt If you have any sub folders give like sample/test.txt . 如果你有任何子文件夹给出像sample/test.txt Kindly check the below code, it is use full for some one like me. 请检查以下代码,对于像我这样的人来说,它是完整的。

/* Source File Name and Path */
            $remote_file = '/var/www/html/artbak/assets/uploads/edi/test.txt';

            $ftp_host = 'hostname'; /* host */
            $ftp_user_name = 'username'; /* username */
            $ftp_user_pass = 'password'; /* password */ 

            /* New file name and path for this file */
            $local_file = 'test.txt';

            /* 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 );

            /* Download $remote_file and save to $local_file */
            if ( ftp_put( $connect_it, $local_file, $remote_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 );

And one thing is in my server had some firewall block, for that reason only first ftp not connecting via php code. 有一件事是在我的服务器上有一些防火墙块,因此只有第一个ftp没有通过PHP代码连接。 Now i solve that issue also. 现在我也解决了这个问题。 Because my code working in local to server already know. 因为我在本地工作的代码已经知道了。 For that i referred the below link to solve the firewall block in server, Can't connect to FTP with PHP ftp_connect from localhost . 为此,我引用了以下链接来解决服务器中的防火墙块, 无法使用来自localhost的PHP ftp_connect连接到FTP So it is working well. 所以它运作良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM