2013-02-12 32 views
2

我想使用像here这样的功能。请检查代码下面的代码stream_context_set_params不适用于ssh2.sftp包装

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:[email protected]:port/path/to/file", 
      false, $ctx); 

,它工作正常!通知功能被多次调用。 我尝试使用SFTP包装这样

$data = file_get_contents("ssh2.sftp://username:[email protected]:port/path/to/big/file", 
      false, $ctx); 

它isn`t工作过。 任何想法?

+0

我不知道,但我想这也是SSH的概念之一。加密连接处理由ssh lib完成,而不是由php完成。因此没有通知。将调查,如果这是真的 – hek2mgl 2013-02-12 11:59:03

+0

是的。似乎它不应该为ssh和ssh包装(scp,tunnel,sftp等)工作 – Stanislav 2013-02-12 12:38:44

+0

是的。我也测试过了。顺便说一句,我在php-ssh2扩展中发现了一个段错误。 :| ...我目前正在查看扩展代码,以了解为什么不会调用通知。要么是通过设计,要么就是没有实施 – hek2mgl 2013-02-12 12:41:02

回答

2

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。