1

我正在尝试结合 ssh2_connect 实现 php,以便使用以下脚本在远程文件夹上解压缩:

$connection = ssh2_connect($ipaddress, 22);

    if (!$connection) {
        throw new Exception("Could not connect to server.");
    }

    if (!ssh2_auth_password($connection, $username, $password)) {
        throw new Exception("Authentication failed!");
    }
    else {
        $unzip = "unzip $filename";
        //$unzip = "unzip $filename -d /home/upload/homes/folder/";
        if ( ($stream = ssh2_exec( $connection, $unzip ) ) ){
            $sftp = ssh2_sftp($connection);
            ssh2_sftp_unlink($sftp, $filename);
        }
    }

我的 zip 文件包含多个文件,但 unzip 函数只返回 1 个 0kB 的文件。当我使用 ssh 登录时,我可以成功解压缩文件。有任何想法吗?

4

1 回答 1

1

libssh2 的行为很奇怪。phpseclib 是一个纯 PHP SSH2 实现,您可能会更幸运。例如。

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec("unzip $filename")
?>

如果这不起作用,您可以获取 phpseclib 日志并将其发布以进行进一步诊断。

于 2015-03-09T18:13:33.047 回答