php - Stream_context_set_params 不适用于 ssh2.sftp 包装器

标签 php sftp libssh2

我想使用类似 here 的功能。请检查以下代码

function notify (
    $notification_code,
    $severity,
    $message,
    $message_code,
    $bytes_transferred,
    $bytes_max
) {
    echo "Runned \n";
};

$ctx = stream_context_create();
stream_set_params($ctx, array('notification' => 'notify'));
$ssh_connection = ssh2_connect('myhost');
ssh2_auth_password($ssh_connection, 'login','pass');
$sftp_resource = ssh2_sftp($ssh_connection);
$data = file_get_contents("ssh2.sftp://{$sftp_resource}/path/to/big/file",
            false, $ctx);

我希望我的通知函数至少被调用一次。 实际上,相同的代码适用于 ftp 包装器

function notify (
    $notification_code,
    $severity,
    $message,
    $message_code, 
    $bytes_transferred,
    $bytes_max
) {
    echo "Runned \n";
};

$ctx = stream_context_create();
stream_set_params($ctx, array('notification' => 'notify'));
$scheme = 'ftp';
$data = file_get_contents("{scheme}://username:password@host:port/path/to/file",
            false, $ctx);

而且效果很好!通知函数被调用多次。 我尝试像这样使用 sftp 包装器

$data = file_get_contents("ssh2.sftp://username:password@host:port/path/to/big/file",
            false, $ctx);

而且它也不起作用。 有什么想法吗?

最佳答案

ssh2 扩展不支持通知回调。我不知道这是设计使然还是只是没有实现,但扩展代码缺少对以下函数的调用:

来自 (PHP-5.4.10)/ext/standard/ftp_fopen_wrapper.c,第 573 行:

php_stream_notify_progress_init(context, 0, file_size);

一个解决方法(我尚未测试过)可能是使用ftps://(通过 ssl 的 FTP)。它应该满足您的安全需求,并且 - 正如代码所示 - 将支持 ftp 通知。详细来说,它使用与 ftp 相同的 urlwrapper。

关于php - Stream_context_set_params 不适用于 ssh2.sftp 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14831620/

相关文章:

php - 检查页面是否在浏览器中查看? PHP

php - CodeIgniter - MySql 时间戳/日期时间在插入时不断给我零

javascript - 使用不同页面的服务而不显示它

node.js - Node 中用于下载和删除文件的 SFTP 模块

linux - 带有 libssh2 的 libcurl - 一个或多个链接时可用的库在运行时不可用

php - 输出缓冲用例作为 "headers already sent"的正确解决方案

java - Apache Mina Sshd SetPasswordAuthenticator 不执行

autocomplete - 使用 sftp 自动完成选项卡

phpseclib 或 ssh2 pecl 扩展

ios - 通过 NMSSH 的数据流