文件上传并保存在另一台服务器PHP上

时间:2018-04-12 03:37:00

标签: php file-upload

我目前在一台服务器(服务器A)上使用表单收集和上传图像,我想将其存储在另一台服务器(服务器B)上。从我看到的例子中,我需要保存文件,然后使用副本或卷曲复制它。

我想获取我在服务器A上收到的文件并将其发送到服务器B而不实际将文件保存在服务器A上。有没有办法让我这样做? 我尝试将文件转换为dataURI字符串并以这种方式发送,但是我认为字符串太长而且我遇到了错误。 提前感谢您的帮助!

更新 我找到了解决方案爱好者! 上传时,该文件存储在临时存储中。 您不能仅使用文件名将文件保存在其他服务器上,但您可以使用该信息创建CurlFile,然后将其传递到您要创建的其他服务器上的页面(请参阅Shivani Patel对{的回复{3}})。这与PHP: upload file from one server to another server的信息相结合,我的代码正常运行。

如果合适,请标记为重复。在建议之前我没想过要查看临时文件名:)

1 个答案:

答案 0 :(得分:0)

您可以使用ftp_put()功能,如下所示

// information of second server
$ftp_server = "ftp.example.com";
// name file in serverA that you want to store file in serverB
$file = 'somefile.png';
$remote_file = 'serverB_file.png';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// 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";
}

我也做了同样的事情..首先我在serverA中移动文件然后在serverB中传输,,,完成传输后取消链接serverA中的文件。