使用php不工作将文件从一台服务器上传到另一台服务器?
问题描述:
我已经在很多方面尝试使用PHP中的FTP功能上传文件。但它不适合我。我不知道我在这段代码中犯了什么错误。我已经给出了本地文件路径或另一个服务器文件路径,但在这两种情况下它都不起作用。下面给出了我的代码。任何人都可以帮助找到我的问题?使用php不工作将文件从一台服务器上传到另一台服务器?
/* 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);
以下代码适用于本地到服务器的上载,但不适用于服务器到服务器的上载。它显示服务器未连接。请给一些想法家伙。我在这个概念上挣扎得更多。
$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);
答
感谢您的支持人员。我很高兴发布这个答案几天的工作。只需要避开目标服务器路径/public_html/test.txt
而不是test.txt
。如果你有任何子文件夹给像sample/test.txt
。请检查下面的代码,对于像我这样的人来说,它是完整的。
/* 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);
还有一件事是在我的服务器有一些防火墙模块,因为这个原因通过PHP代码只有第一个FTP连接不。现在我也解决了这个问题。因为我在本地服务器上的代码已经知道了。为此,我参考了下面的链接来解决服务器中的防火墙块,Can't connect to FTP with PHP ftp_connect from localhost。所以它运作良好。
给定的FTP详细信息是目标服务器,它是正确的或我需要源服务器的详细信息? –
尝试使用'error_reporting(E_ALL)'开启错误,并在'else'分支中输出'error_get_last()'。 – cmbuckley
好吧,我会检查它。错误显示为ftp_login(),ftp_put()和ftp_close()期望参数1为资源,布尔给定。 –