Revetahw
0
Q:

sftp copy file from local to remote

$srcFile = $this->csvFilePath.'/'.$filename;
        $dstFile = $this->serverPutPath.'/'.$filename;

        $conn = ssh2_connect($this->server, $this->serverPort);
        ssh2_auth_password($conn, $this->serverUser, $this->serverPassword);
         
        // Create SFTP session
        $sftp = ssh2_sftp($conn);
         
 
        $sftpStream = fopen("ssh2.sftp://".intval($sftp) .$dstFile, 'w');
         
        try {
         
            if (!$sftpStream) {
                throw new Exception("Could not open remote file: $dstFile");
            }
         
            $data_to_send = file_get_contents($srcFile);
         
            if ($data_to_send === false) {
                throw new Exception("Could not open local file: $srcFile.");
            }
         
            if (fwrite($sftpStream, $data_to_send) === false) {
                throw new Exception("Could not send data from file: $srcFile.");
            }
         
            fclose($sftpStream);
         
        } catch (Exception $e) {
            error_log('Exception: ' . $e->getMessage());
            fclose($sftpStream);
        }
0

New to Communities?

Join the community