cvs: phpdoc /en/reference/stream/functions stream-notification-callback.xml

From: Date: Mon, 14 Apr 2008 13:37:06 +0000
Subject: cvs: phpdoc /en/reference/stream/functions stream-notification-callback.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-1495@lists.php.net to get a copy of this message
bjori Mon Apr 14 13:37:06 2008 UTC Modified files: /phpdoc/en/reference/stream/functions stream-notification-callback.xml Log: Add download progressbar example http://cvs.php.net/viewvc.cgi/phpdoc/en/reference/stream/functions/stream-notification-callback.xml?r1=1.3&r2=1.4&diff_format=u Index: phpdoc/en/reference/stream/functions/stream-notification-callback.xml diff -u phpdoc/en/reference/stream/functions/stream-notification-callback.xml:1.3 phpdoc/en/reference/stream/functions/stream-notification-callback.xml:1.4 --- phpdoc/en/reference/stream/functions/stream-notification-callback.xml:1.3 Mon Apr 14 08:44:22 2008 +++ phpdoc/en/reference/stream/functions/stream-notification-callback.xml Mon Apr 14 13:37:06 2008 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<!-- $Revision: 1.3 $ --> +<!-- $Revision: 1.4 $ --> <refentry xml:id="function.stream-notification-callback" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> <refnamediv> @@ -173,6 +173,95 @@ </screen> </example><!-- }}} --> </para> + <para> + <example xml:id="stream-notification-callback.example.download"><!-- {{{ --> + <title>Simple progressbar for commandline download client</title> + <programlisting role="php"> +<![CDATA[ +<?php +function usage($argv) { + echo "Usage:\n"; + printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]); + exit(1); +} + +function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { + static $filesize = null; + + switch($notification_code) { + case STREAM_NOTIFY_RESOLVE: + case STREAM_NOTIFY_AUTH_REQUIRED: + case STREAM_NOTIFY_COMPLETED: + case STREAM_NOTIFY_FAILURE: + case STREAM_NOTIFY_AUTH_RESULT: + /* Ignore */ + break; + + case STREAM_NOTIFY_REDIRECTED: + echo "Being redirected to: ", $message, "\n"; + break; + + case STREAM_NOTIFY_CONNECT: + echo "Conntected...\n"; + break; + + case STREAM_NOTIFY_FILE_SIZE_IS: + $filesize = $bytes_max; + echo "Filesize: ", $filesize, "\n"; + break; + + case STREAM_NOTIFY_MIME_TYPE_IS: + echo "Mime-type: ", $message, "\n"; + break; + + case STREAM_NOTIFY_PROGRESS: + if ($bytes_transferred > 0) { + if (!isset($filesize)) { + printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024); + } else { + $length = (int)(($bytes_transferred/$filesize)*100); + printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024); + } + } + break; + } +} + +isset($argv[1], $argv[2]) or usage($argv); + +$ctx = stream_context_create(null, array("notification" => "stream_notification_callback")); + +$fp = fopen($argv[1], "r", false, $ctx); +if (is_resource($fp) && file_put_contents($argv[2], $fp)) { + echo "\nDone!\n"; + exit(0); +} + +$err = error_get_last(); +echo "\nErrrrrorr..\n", $err["message"], "\n"; +exit(1); +?> +]]> + </programlisting> + <para> + Executing the example above with: + <literal>php -n fetch.php + http://no2.php.net/get/php-5-LATEST.tar.bz2/from/this/mirror + php-latest.tar.bz2</literal> will output something similar too: + </para> + <screen> +<![CDATA[ +Conntected... +Mime-type: text/html; charset=utf-8 +Being redirected to: http://no2.php.net/distributions/php-5.2.5.tar.bz2 +Conntected... +Filesize: 7773024 +Mime-type: application/octet-stream +[========================================> ] 40% (3076/7590 kb) +]]> + </screen> + </example> + </para> </refsect1><!-- }}} --> <refsect1 role="seealso"><!-- {{{ -->

« previous php.doc.cvs (#1495) next »