php有没有方法能获取上下文,php-未调用流上下文中的通知

我在php(wordpress)中使用此代码检查下载进度:

// Create context

$context = stream_context_create();

stream_context_set_params( $context, [ 'notification' => 'my_stream_notification_callback' ] );

// Declare progress function

function my_stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {

print_r(func_get_args());

}

// Call download

$wp_upload_dir = wp_upload_dir();

file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r' ), null, $context );

代码确实在中成功下载了文件

/wp-content/uploads/

文件夹,但不打印任何通知/进度。

我也试着写信给

error_log()

my_stream_notification_callback()

函数,但它不在其中写入任何内容,

debug.log

文件是空的。这意味着根本没有调用通知回调。

有人知道为什么会这样吗?

下面是一个代码相似但问题不同的问题:

Download files file_put_contents with progress

显然,在为类/对象方法函数修复回调函数后,代码对他有效。当我尝试一个更简单的回调函数时,它肯定会起作用。

有什么想法吗?

--编辑——

我用了那个例子,它似乎有效。

请参阅我使用的代码(与示例略有不同):

$ctx = stream_context_create();

stream_context_set_params( $ctx, [

"notification" => function ( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max ) {

print_r(func_get_args());

},

] );

file_get_contents( "http://php.net/contact", false, $ctx );

现在,此代码工作并打印出进度通知:

这是不是意味着

file_put_contents()

fopen()

存在上下文/通知问题

file_get_contents()

没有?

--编辑——

以下是实际工作的变化:

file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r', null, $context ) );

表示,而不是将$context应用于

文件放置目录()

我们应用于

FOpen-()

打电话就行了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值