PHP SSH2 文件上传在 15kb 处挂起

问题描述 投票:0回答:1

我正在尝试使用 SFTP 上传文件。最近从 PHP 7.4 升级。我在 7.4 中编写的相同代码不再有效。每次我尝试上传文件时,它都会停在 15KB 处。

如果我创建一个没有数据的新文件,该函数将完成调用。我得到一个 0KB 文件。我也可以毫无问题地下载文件。

我在 IISv10 上使用 PHP 8.1.26。 我使用的SSH2版本是1.3.1 https://pecl.php.net/package/ssh2/1.3.1/windows

这是我用来测试这个的代码块。我知道代码很糟糕,但这只是一个测试。你可以看到我已经尝试了很多事情。我已经确认我没有 SCP 访问权限。

function ssh2sendtest($sshHost, $sshPort, $sshUsername, $sshPassword, $localFilePath) {

    // Encryption methods for SSH connection (unnecessary)
    $cryptoMethods = [
        "client_to_server" => [
            "crypt" => "aes256-ctr,aes256-cbc,[email protected],aes192-cbc,aes128-ctr,3des-cbc"
        ],
        "server_to_client" => [
            "crypt" => "aes256-ctr,aes256-cbc,[email protected],aes192-cbc,aes128-ctr,3des-cbc"
        ]
    ];

    // Define the callback functions (does nothing)
    $callbacks = array(
        'disconnect' => function ($reason, $message, $language) {
            echo "Disconnected with reason code [$reason] and message: $message\n";
        },
        'debug'      => function ($message, $language, $always_display) {
            echo "Debug message: $message\n";
        },
        'ignore'     => function ($message) {
            echo "Ignore message: $message\n";
        },
        'macerror'   => function ($packet) {
            echo "MAC error on packet: " . bin2hex($packet) . "\n";
            return false; // Return false to disconnect if there's a MAC error
        }
    );

    // Establishing SSH connection
    $connection = ssh2_connect($sshHost, $sshPort); //, $cryptoMethods, $callbacks);
    if (!$connection) {
        return 'Connection failed';
        //exit;
    }

    // Authentication
    if (!ssh2_auth_password($connection, $sshUsername, $sshPassword)) {
        return 'Authentication failed';
        //exit;
    }

    // Initializing SFTP session
    $sftp = ssh2_sftp($connection);
    if (!$sftp) {
        return 'SFTP session failed';
        //exit;
    }

    // Remote file path
    $uniqueHash     = md5(uniqid(rand(), true));
    $remoteFilePath = "/KSDOA/Inbound/test{$uniqueHash}.txt";
    $openFile = fopen($localFilePath, "r");
    file_put_contents("ssh2.sftp://{$sshUsername}:{$sshPassword}@{$sshHost}:22/{$remoteFilePath}", $openFile);

    /*
    // Opening remote file in write mode
     $remoteFile = fopen("ssh2.sftp://{$sftp}{$remoteFilePath}", 'w');
     if (!$remoteFile) {
         return 'Failed to open remote file';
     }

     $localData = file_get_contents($localFilePath);
    $writeLength = strlen($localData);
     // Writing to the remote file
     fwrite($remoteFile, $localData, $writeLength);
     fflush($remoteFile);  // Flush the output buffer

     fclose($remoteFile);

    // Closing the connection
    ssh2_disconnect($connection);
    */
    return "File written and command executed successfully.";
}

我正在使用的服务器能够发送文件,因为在这次堆栈更新之前我已经发送文件很长时间了。我还可以使用 CoreFTP 手动上传文件。

我做错了什么?我找不到我的堆栈或代码有任何问题(除了我必须在 Windows 上支持它这一事实)。

编辑-- 虽然我不明白为什么现在需要缓冲区,但这确实有效。

    // Preparing to write to the remote file
    $remoteStream = fopen("ssh2.sftp://{$sftp}{$remoteFilePath}", 'w');
    if (!$remoteStream) {
        fclose($localStream);
        die('Cannot open remote file');
    }
    
    // Reading from the local file and writing to the remote file
    while ($buffer = fread($localStream, 4096)) {
        fwrite($remoteStream, $buffer);
    }

    fclose($localStream);
    fclose($remoteStream);
    ssh2_disconnect($connection);

读取整个文件不起作用。这不是内存限制问题,文件约为 35k。这不行。

    // Preparing to write to the remote file
    $remoteStream = fopen("ssh2.sftp://{$sftp}{$remoteFilePath}", 'w');
    if (!$remoteStream) {
        fclose($localStream);
        die('Cannot open remote file');
    }

    $localData = file_get_contents($localFilePath);
    fwrite($remoteStream, $localData);

    fclose($remoteStream);
    ssh2_disconnect($connection);
php sftp ssh2 ssh2-sftp
1个回答
0
投票

答案已发布在编辑中。很快也会尝试奥利维尔的建议。

© www.soinside.com 2019 - 2024. All rights reserved.