I have one requirement in which i need to get zip file from remote sftp and read its data after extract it.

I see there is class Magento\Framework\Filesystem\Io\Sftp which i can use like below.

   \Magento\Framework\Filesystem\Io\Sftp $ftp
       $this->ftp = $ftp;

   public function uploadFile(){
       $open = $this->ftp->open(
                       'host' => 'hostname here',
                       'username' => 'username',
                       'password' => 'password'

but how to retrieve files and extract it so i can copy to my server.

1 Answer 1


You can try using the read() function defined in that same class:

 * Read a file
 * @param string $filename remote file name
 * @param string|null $destination local file name (optional)
 * @return mixed
public function read($filename, $destination = null)
    if ($destination === null) {
        $destination = false;
    return $this->_connection->get($filename, $destination);

And the code to copy a remote file to your local would be like:

$result = $this->ftp->read('/path/to/remote/filename', '/path/to/local/filename');

$result here is a boolean, which can be used to determine whether it's successful or not.

  • Hi, But file is compressed .gz file. I guess i need to extract before read it right?
    – Kul
    Dec 19, 2018 at 11:50
  • you can extract it after it has been transferred to your local. Dec 19, 2018 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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