问题
I want to use functionality like here. Please check the code bellow
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);
I expect that my notify function will be called at least once. Actually, the same code works for ftp wrappers
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);
And it works fine! The notify function is called many times. I try to use sftp wrapper like this
$data = file_get_contents("ssh2.sftp://username:password@host:port/path/to/big/file",
false, $ctx);
And it isn`t works too. Any ideas?
回答1:
The ssh2 extension doesn't support notfication callbacks. I don't know if this is by design or just not implemented, but the extensions code is missing calls to functions like:
From (PHP-5.4.10) /ext/standard/ftp_fopen_wrapper.c, line 573:
php_stream_notify_progress_init(context, 0, file_size);
A workaround, which I've not tested yet might be to use ftps://
(FTP over ssl). It should fit your security needs and - as the code looks like - will support notifications as ftp. In detail, it uses the same urlwrapper as ftp.
回答2:
Looks like a recent commit to phpseclib's pure PHP SFTP stream wrapper adds support for notifications:
https://github.com/phpseclib/phpseclib/commit/a47c1c39809a18b870b3812ce9ab84c7bd55efdd
来源:https://stackoverflow.com/questions/14831620/stream-context-set-params-not-works-with-ssh2-sftp-wrapper