In my project, I use sftp (https://www.php.net/manual/en/book.ssh2.php) to upload file to remote server.
Code upload file:
if (!$localStream = @fopen($localFilePath, 'r')) {
throw new Exception("Unable to open local file for reading: $localFilePath");
}
// Remote stream
if (!$remoteStream = @fopen("ssh2.sftp://".$sftp_int."/$savePath", 'w')) {
throw new Exception("Unable to open remote file for writing: $savePath");
}
// Write from our remote stream to our local stream
$read = 0;
$fileSize = filesize($localFilePath);
while ($read < $fileSize && ($buffer = fread($localStream, $fileSize - $read))) {
// Increase our bytes read
$read += strlen($buffer);
// Write to our local file
if (fwrite($remoteStream, $buffer) === false) {
throw new Exception("Unable to write to local file: $savePath");
}
}
But I tracking & see speed upload slow.
I research and found: Why is SSH2 SFTP so much slower than FTP and FTPS?
And: http://phpseclib.sourceforge.net/
How to increase upload speed using sftp with PHP?
Please, help me. Thanks