1<?php 2 3function usage($argv) { 4 echo "Usage:\n"; 5 printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]); 6 exit(1); 7} 8 9function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { 10 static $filesize = null; 11 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: 18 /* Ignore */ 19 break; 20 21 case STREAM_NOTIFY_REDIRECTED: 22 echo "Being redirected to: ", $message, "\n"; 23 break; 24 25 case STREAM_NOTIFY_CONNECT: 26 echo "Connected...\n"; 27 break; 28 29 case STREAM_NOTIFY_FILE_SIZE_IS: 30 $filesize = $bytes_max; 31 echo "Filesize: ", $filesize, "\n"; 32 break; 33 34 case STREAM_NOTIFY_MIME_TYPE_IS: 35 echo "Mime-type: ", $message, "\n"; 36 break; 37 38 case STREAM_NOTIFY_PROGRESS: 39 if ($bytes_transferred > 0) { 40 if (!isset($filesize)) { 41 printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024); 42 } else { 43 $length = (int)(($bytes_transferred/$filesize)*100); 44 printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024); 45 } 46 } 47 break; 48 } 49} 50 51isset($argv[1], $argv[2]) or usage($argv); 52 53if (!isset($_ENV['http_proxy'])) { 54 $copt = null; 55} else { 56 $copt = array( 57 'http' => array( 58 'proxy' => preg_replace('/^http/i', 'tcp', $_ENV['http_proxy']), 59 'request_fulluri' => true, 60 ), 61 ); 62} 63 64$ctx = stream_context_create($copt, array("notification" => "stream_notification_callback")); 65 66$fp = fopen($argv[1], "r", false, $ctx); 67if (is_resource($fp) && file_put_contents($argv[2], $fp)) { 68 echo "\nDone!\n"; 69 exit(0); 70} 71 72$err = error_get_last(); 73echo "\nError..\n", $err["message"], "\n"; 74exit(1); 75