1、初始化对象:\Magento\Framework\Filesystem\Io\Ftp
protected $ftp; public function __construct( \Magento\Framework\Filesystem\Io\Ftp $ftp ){ $this->ftp = $ftp; }
2、Build the FTP Connect
$open = $this->ftp->open( array( 'host' => ‘test.localhost.com’, 'user' => ‘test’, 'password' => ‘test’, 'ssl' => '',//true 'passive' => ''//true ) );
Use the written function to upload a file to the server
if ($open) { //方式一:直接读一个文件上传 $fileName = ‘test.csv’; $content = file_get_contents(DirectoryList::VAR_DIR . '/' . $fileName); $this->ftp->write(self::FILE_NAME_ON_FTP, $content); $this->ftp->close(); //方式二:创建文件并一行行写 $fileName = 'test.csv'; $content = "姓名,年龄,班级\n张三,18,大一"; $result = $this->ftp->write($fileName, $content); $this->ftp->close(); }