|
| 1 | +<?php |
| 2 | +function usage($argv) { |
| 3 | + echo "Usage:\n"; |
| 4 | + printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]); |
| 5 | + exit(1); |
| 6 | +} |
| 7 | + |
| 8 | +function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { |
| 9 | + static $filesize = null; |
| 10 | + |
| 11 | + switch($notification_code) { |
| 12 | + case STREAM_NOTIFY_RESOLVE: |
| 13 | + case STREAM_NOTIFY_AUTH_REQUIRED: |
| 14 | + case STREAM_NOTIFY_COMPLETED: |
| 15 | + case STREAM_NOTIFY_FAILURE: |
| 16 | + case STREAM_NOTIFY_AUTH_RESULT: |
| 17 | + /* Ignore */ |
| 18 | + break; |
| 19 | + |
| 20 | + case STREAM_NOTIFY_REDIRECTED: |
| 21 | + echo "Being redirected to: ", $message, "\n"; |
| 22 | + break; |
| 23 | + |
| 24 | + case STREAM_NOTIFY_CONNECT: |
| 25 | + echo "Conntected...\n"; |
| 26 | + break; |
| 27 | + |
| 28 | + case STREAM_NOTIFY_FILE_SIZE_IS: |
| 29 | + $filesize = $bytes_max; |
| 30 | + echo "Filesize: ", $filesize, "\n"; |
| 31 | + break; |
| 32 | + |
| 33 | + case STREAM_NOTIFY_MIME_TYPE_IS: |
| 34 | + echo "Mime-type: ", $message, "\n"; |
| 35 | + break; |
| 36 | + |
| 37 | + case STREAM_NOTIFY_PROGRESS: |
| 38 | + if ($bytes_transferred > 0) { |
| 39 | + if (!isset($filesize)) { |
| 40 | + printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024); |
| 41 | + } else { |
| 42 | + $length = (int)(($bytes_transferred/$filesize)*100); |
| 43 | + printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024); |
| 44 | + } |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +isset($argv[1], $argv[2]) or usage($argv); |
| 51 | + |
| 52 | +$ctx = stream_context_create(null, array("notification" => "stream_notification_callback")); |
| 53 | + |
| 54 | +$fp = fopen($argv[1], "r", false, $ctx); |
| 55 | +if (is_resource($fp) && file_put_contents($argv[2], $fp)) { |
| 56 | + echo "\nDone!\n"; |
| 57 | + exit(0); |
| 58 | +} |
| 59 | + |
| 60 | +$err = error_get_last(); |
| 61 | +echo "\nErrrrrorr..\n", $err["message"], "\n"; |
| 62 | +exit(1); |
| 63 | + |
| 64 | + |
0 commit comments