Back to index

php5  5.3.10
fetch.php
Go to the documentation of this file.
00001 <?php
00002 
00003 function usage($argv) {
00004     echo "Usage:\n";
00005     printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]);
00006     exit(1);
00007 }
00008 
00009 function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
00010     static $filesize = null;
00011 
00012     switch($notification_code) {
00013     case STREAM_NOTIFY_RESOLVE:
00014     case STREAM_NOTIFY_AUTH_REQUIRED:
00015     case STREAM_NOTIFY_COMPLETED:
00016     case STREAM_NOTIFY_FAILURE:
00017     case STREAM_NOTIFY_AUTH_RESULT:
00018         /* Ignore */
00019         break;
00020 
00021     case STREAM_NOTIFY_REDIRECTED:
00022         echo "Being redirected to: ", $message, "\n";
00023         break;
00024 
00025     case STREAM_NOTIFY_CONNECT:
00026         echo "Connected...\n";
00027         break;
00028 
00029     case STREAM_NOTIFY_FILE_SIZE_IS:
00030         $filesize = $bytes_max;
00031         echo "Filesize: ", $filesize, "\n";
00032         break;
00033 
00034     case STREAM_NOTIFY_MIME_TYPE_IS:
00035         echo "Mime-type: ", $message, "\n";
00036         break;
00037 
00038     case STREAM_NOTIFY_PROGRESS:
00039         if ($bytes_transferred > 0) {
00040             if (!isset($filesize)) {
00041                 printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024);
00042             } else {
00043                 $length = (int)(($bytes_transferred/$filesize)*100);
00044                 printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024);
00045             }
00046         }
00047         break;
00048     }
00049 }
00050 
00051 isset($argv[1], $argv[2]) or usage($argv);
00052 
00053 if (!isset($_ENV['http_proxy'])) {
00054     $copt = null;
00055 } else {
00056     $copt = array(
00057         'http' => array(
00058             'proxy' => preg_replace('/^http/i', 'tcp', $_ENV['http_proxy']),
00059             'request_fulluri' => true,
00060         ),
00061     );
00062 }
00063 
00064 $ctx = stream_context_create($copt, array("notification" => "stream_notification_callback"));
00065 
00066 $fp = fopen($argv[1], "r", false, $ctx);
00067 if (is_resource($fp) && file_put_contents($argv[2], $fp)) {
00068     echo "\nDone!\n";
00069     exit(0);
00070 }
00071 
00072 $err = error_get_last();
00073 echo "\nError..\n", $err["message"], "\n";
00074 exit(1);
00075 
00076