我想使用类似 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/