3 function usage($argv) {
5 printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]);
9 function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
10 static $filesize = null;
12 switch($notification_code) {
13 case STREAM_NOTIFY_RESOLVE
:
14 case STREAM_NOTIFY_AUTH_REQUIRED
:
15 case STREAM_NOTIFY_COMPLETED
:
16 case STREAM_NOTIFY_FAILURE
:
17 case STREAM_NOTIFY_AUTH_RESULT
:
21 case STREAM_NOTIFY_REDIRECTED
:
22 echo "Being redirected to: ", $message, "\n";
25 case STREAM_NOTIFY_CONNECT
:
26 echo "Connected...\n";
29 case STREAM_NOTIFY_FILE_SIZE_IS
:
30 $filesize = $bytes_max;
31 echo "Filesize: ", $filesize, "\n";
34 case STREAM_NOTIFY_MIME_TYPE_IS
:
35 echo "Mime-type: ", $message, "\n";
38 case STREAM_NOTIFY_PROGRESS
:
39 if ($bytes_transferred > 0) {
40 if (!isset($filesize)) {
41 printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024);
43 $length = (int)(($bytes_transferred/$filesize)*100);
44 printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024);
51 isset($argv[1], $argv[2]) or usage($argv);
53 if (!isset($_ENV['http_proxy'])) {
58 'proxy' => preg_replace('/^http/i', 'tcp', $_ENV['http_proxy']),
59 'request_fulluri' => true,
64 $ctx = stream_context_create($copt, array("notification" => "stream_notification_callback"));
66 $fp = fopen($argv[1], "r", false, $ctx);
67 if (is_resource($fp) && file_put_contents($argv[2], $fp)) {
72 $err = error_get_last();
73 echo "\nError..\n", $err["message"], "\n";