stream_notification_callback() - php 流数据函数stream
stream_notification_callback()
(PHP 5 >= 5.2.0, PHP 7)
A callback function for thenotificationcontext parameter
说明
stream_notification_callback(int $notification_code,int $severity,string $message,int $message_code,int $bytes_transferred,int $bytes_max): voidAcallablefunction, used by thenotification context parameter, called during an event.
Note:This isnota real function, only a prototype of how the function should be.
参数
$notification_codeOne of theSTREAM_NOTIFY_*
notification constants.
One of theSTREAM_NOTIFY_SEVERITY_*
notification constants.
Passed if a descriptive message is available for the event.
$message_codePassed if a descriptive message code is available for the event.
The meaning of this value is dependent on the specific wrapper in use.
$bytes_transferredIf applicable,the$bytes_transferredwill be populated.
$bytes_maxIf applicable,the$bytes_maxwill be populated.
返回值
没有返回值。
范例
Example #1stream_notification_callback()example
以上例程的输出类似于:
Connected... Found the mime-type: text/html; charset=utf-8 Being redirected to: http://no.php.net/contact Connected... Got the filesize: 0 Found the mime-type: text/html; charset=utf-8 Being redirected to: http://no.php.net/contact.php Connected... Got the filesize: 4589 Found the mime-type: text/html;charset=utf-8 Made some progress, downloaded 0 so far Made some progress, downloaded 0 so far Made some progress, downloaded 0 so far Made some progress, downloaded 1440 so far Made some progress, downloaded 2880 so far Made some progress, downloaded 4320 so far Made some progress, downloaded 5760 so far Made some progress, downloaded 6381 so far Made some progress, downloaded 7002 so far
Simple progressbar for commandline download client
Executing the example above with:php -n fetch.php http://no2.php.net/get/php-5-LATEST.tar.bz2/from/this/mirror php-latest.tar.bz2will output something similar too:
Connected... Mime-type: text/html; charset=utf-8 Being redirected to: http://no2.php.net/distributions/php-5.2.5.tar.bz2 Connected... Filesize: 7773024 Mime-type: application/octet-stream [========================================> ] 40% (3076/7590 kb)
参见
- Context 参数
Please note that the callback function never gets called when the stream is (at least) of type socket. It is probably the same for the file:// proto although I've not done further tests. I'm using php 5.3.3.
A couple of notes regarding the examples here (I'm using PHP 7.0.18): 1) fopen() did not provide STREAM_NOTIFY_PROGRESS for me but `file_get_contents()` worked fine. 2) The bytes transferred when transferring a zip file seems to always be shy by 8192 (8k). This varies depending on filetype and also breaks STREAM_NOTIFY_PROGRESS when the filesize is below 8k. I modified the function and it's now displaying the correct amount transferred and percentage downloaded on zip files above 8k. I'm not sure why the callback is so inconsistent but hopefully this is helpful to someone. This was also modified to only display progress:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!