如何解决如何在 Magento 2 中使用 sftp 将文件从一台服务器传输到另一台服务器?
我想使用 sftp 将与该产品关联的所有图像从 M1 传输到 M2。我可以使用 sftp 连接到 M1。但我不知道如何转移它。
这里是连接sftp的代码-
//FTP Connection
public function connectFtp($host,$user,$password,$ssl=true,$passive=true){
return $connect = $this->sftp->open(
array(
'host' => $host,'user' => $user,'password' => $password,'ssl' => $ssl,'passive' => $passive
)
);
}
//Downlaod images from M1 and transfer to M2 temp folder
public function downloadImages($images){
//Connecting to M1
$connect = $this->connectFtp(SELF::M1_HOST,SELF::M1_USERNAME,SELF::M1_PASSWORD,SELF::M1_SSL,SELF::M1_PASSIVE);
if($connect){
/* Code to transfer */
}
}
如何实现这种文件传输?根据一些项目要求,我们不想使用任何插件。
解决方法
您是否只想将所有图像从 M1 传输到 M2?
您可以尝试 rsync 图像:
rsync user@m1.host:/path/to/copy user@m2.host:/path/to/copy
,
文件从 SFTP 服务器传输到我们的服务器的工作代码。
$sftp = $obj->create('Magento\Framework\Filesystem\Io\Sftp');
$open = $sftp->open(
array(
'host' => $sftp_hostname,'username' => $sftp_username,'password' => $sftp_password,'port'=>$sftp_port
)
);
try{
// Change directory and move to require directory
$sftp->cd('/enter_your_sftp_file_path_here/');
//Fetching/Listing all the files.
$sftp_server_files = $sftp->ls();
//File path to put files from SFTP Server to our local server
$filepath = $dir->getRoot() . '/var/import/inventory/';
foreach ($sftp_server_files as $file) {
//Copy files from server
$source = $file['text'];
if(strpos($source,'zip') !== false){
$destination = $filepath.$source;
$result = $sftp->read($source,$destination);
if($result == 'true') {
$message[] = 'File read from SFTP server : '.$source;
}
else
{
$message[] = 'File not able to read from SFTP server : '.$source;
}
}
}
} catch ( Exception $e) {
echo $e->getMessage();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。