Skip to content

Commit

Permalink
SFTP: speed up uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Feb 26, 2020
1 parent db6ce98 commit 08e4096
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions phpseclib/Net/SFTP.php
Expand Up @@ -438,6 +438,9 @@ function __construct($host, $port = 22, $timeout = 10)
if (!defined('NET_SFTP_QUEUE_SIZE')) {
define('NET_SFTP_QUEUE_SIZE', 32);
}
if (!defined('NET_SFTP_UPLOAD_QUEUE_SIZE')) {
define('NET_SFTP_UPLOAD_QUEUE_SIZE', 1024);
}
}

/**
Expand Down Expand Up @@ -2075,7 +2078,7 @@ function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_s
$sftp_packet_size = 4096; // PuTTY uses 4096
// make the SFTP packet be exactly 4096 bytes by including the bytes in the NET_SFTP_WRITE packets "header"
$sftp_packet_size-= strlen($handle) + 25;
$i = 0;
$i = $j = 0;
while ($dataCallback || ($size === 0 || $sent < $size)) {
if ($dataCallback) {
$temp = call_user_func($dataCallback, $sftp_packet_size);
Expand All @@ -2091,7 +2094,7 @@ function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_s

$subtemp = $offset + $sent;
$packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 4294967296, $subtemp, strlen($temp), $temp);
if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet, $j)) {
if ($mode & NET_SFTP_LOCAL_FILE) {
fclose($fp);
}
Expand All @@ -2103,8 +2106,9 @@ function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_s
}

$i++;
$j++;

if ($i == NET_SFTP_QUEUE_SIZE) {
if ($i == NET_SFTP_UPLOAD_QUEUE_SIZE) {
if (!$this->_read_put_responses($i)) {
$i = 0;
break;
Expand Down

1 comment on commit 08e4096

@terrafrost
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1455

Please sign in to comment.