I want to create/write file on another server using Magento

Ex: my site : www.test.com (Magento2site i want to write code here to generate file on www.testTEst.com)

1 Answer 1


You can use ftp or sftp to write file in another server. I have used sftp to create file

namespace Vendor\Module\Model;

class SftpActions
    protected $_sftp;

    public function __construct(
        \Magento\Framework\Filesystem\Io\Sftp $sftp
    ) {
        $this->_sftp = $sftp;

     * @param obj $warehouse
    public function saveFile( $data, $fileName, $exportedFolder )
        $sftp = $this->loginSftp();

        if (!is_dir($exportedFolder))
            $sftp->mkdir($exportedFolder, 0775);
       $this->sftp->cd($exportedFolder);//the path is you will upload path
        $this->sftp->write($fileName, $data); // filename and string data or local file name

     * @return object $sftp
    private function loginSftp()
        $host = 'FTP Host';
        $port = 'FTP port';
        $username = 'username';
        $password = 'password';

                'host' => $host.':'.$port,
                'username' => $username,
                'password' => $password

        return $this->_sftp;

Call saveFile function from anywhere with below params

$data = filedata
$fileName = filename (ex: test.txt)
$exportedFolder = folder path from root of the server

Note: You must enter your host, port, username and password in loginSftp function

  • Instead of $this->sftp in the saveFile function should be $sftp ? Jun 16, 2020 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.