After opening your SFTP connection, you can read files and write using standard PHP functions such as fopen , fread and fwrite . You just need to use the ssh2.sftp:// resource handler to open the remote file.
Here is an example that scans a directory and uploads all files to the root folder:
// Assuming the SSH connection is already established: $resSFTP = ssh2_sftp($resConnection); $dirhandle = opendir("ssh2.sftp://$resSFTP/"); while ($entry = readdir($dirhandle)){ $remotehandle = fopen("ssh2.sftp://$resSFTP/$entry", 'r'); $localhandle = fopen("/tmp/$entry", 'w'); while( $chunk = fread($remotehandle, 8192)) { fwrite($localhandle, $chunk); } fclose($remotehandle); fclose($localhandle); }
source share