2

在我的项目中,我使用 sftp ( https://www.php.net/manual/en/book.ssh2.php ) 将文件上传到远程服务器。

代码上传文件:

    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");
        }
    }

但我跟踪并看到上传速度很慢。

我研究发现:为什么 SSH2 SFTP 比 FTP 和 FTPS 慢这么多?

并且: http: //phpseclib.sourceforge.net/

如何使用 sftp 和 PHP 提高上传速度?

请帮我。谢谢

4

0 回答 0