请高手给看看下面的代码,谢谢了。// 简单的说明下, 代码的作用是:
// 从网上下载文件,然后压缩放入本地。是在命令行下操作的。// Context 获取参数,
// DownloadStream 下载 压缩
<?php
class Context{ 
protected $url;  // 
protected $max_kb_sec; // 最大下载速度
protected $to_file; // 保存到本地的文件名字 public function __construct()
{
if( $_SERVER['argc'] < 2){
echo "\nUsage:\nphp url [max_kb_sec kb/sec] \n\n";
exit();
}else if( $_SERVER['argc'] == 2){
$this->url = $_SERVER['argv'][1];
$this->max_kb_sec = 1000;
}else if( $_SERVER['argc'] == 3){
$this->url = $_SERVER['argv'][1];
$this->max_kb_sec = $_SERVER['argv'][2];
} $url_data = parse_url( $this->url); // 确定保存到本地的文件名
$this->to_file = basename( $url_data['path']);
if( empty( $this->to_file)){
$this->to_file = 'index.html';
}
}

public function getUrl()
{
return $this->url;
} public function getSpeed()
{
return $this->max_kb_sec;
} public function getToFile()
{
return $this->to_file;
}
}// 这个代码是从书上看到的,但是有些地方不明白
class DownloadStream{
protected $context;
protected $term_sol = "\x1b[1G";  // 这里是什么意思?
protected $severity_map = array(
0 => 'info',
1 => 'warning',
2 => 'error'
);
protected $begin_time;
protected $size;

public function __construct( Context $context)
{
$this->context = $context;
$this->init();
} protected function init(){
$context = stream_context_create();
stream_context_set_params( $context, array(
'notification',
array(
$this,
'notifier'
)
));
$fp_read = @fopen( $this->context->getUrl(), 'rb', FALSE, $context); 
if( ! is_resource( $fp_read)){
throw new Exception( "can't read the file\n");
}
$file = './misc/' . $this->context->getToFile() . '.gz';
$fp_write = @fopen( 'compress.zlib://' . $file, 'wb9', FALSE, $context);
if( ! is_resource( $fp_write)){
throw new Exception( "can't write to the file\n");
}
echo "saving to file:" . realpath( $file) . "\n";
while( !feof( $fp_read)){
$data = fread( $fp_read, 1024);
fputs( $fp_write, $data);
}
fclose( $fp_read);
fclose( $fp_write);
printf("{$this->term_sol}[%s] Download time: %ds\n", str_repeat('*', 50), time() - $this->begin_time);
} private function notifier( $code, $severity, $msg, $xcode, $sofar, $max)
{
if( $code != STREAM_NOTIFY_PROGRESS){
echo $this->severity_map[$severity] . ':';
}
switch( $code){
case STREAM_NOTIFY_CONNECT:
echo "Connected\n";
$this->begin_time = time() - 0.001;
break;
case STREAM_NOTIFY_AUTH_REQUIRED:
printf( "Authentication required: %s\n", $msg);
break;
case STREAM_NOTIFY_AUTH_RESULT:
printf( "Logged in: %s\n", trim($msg));
break;
case STREAM_NOTIFY_MIME_TYPE_IS:
printf( "Mime type: %s\n", $msg);
break;
case STREAM_NOTIFY_FILE_SIZE_IS:
printf("Downloading %d kb\n", $max / 1024);
$this->size = $max;
break;
case STREAM_NOTIFY_REDIRECTED:
printf( "Redirecting to %s ...\n", $msg);
break;
case STREAM_NOTIFY_PROGRESS: // 计算 * 和 - 的个数
if( $this->size){
$stars = str_repeat('*', $sofar * 50 / $this->size );
}else{
$stars = '';
}
$stripe = str_repeat( '-', 50 - strlen( $stars)); $kb_sec = ( $sofar / ( time() - $this->begin_time)) / 1024; while( $kb_sec > $this->context->getSpeed() ){ // 限制下载速度
usleep( 1);
$kb_sec = ( $sofar /( time() - $this->begin_time)) / 1024;
}

printf("{$this->term_sol}[%s] %d kb %.1f kb/sec", $stars . $stripe, $sofar / 1024, $kb_sec);
break;
case STREAM_NOTIFY_FAILURE:
printf("Failure: %s\n", $msg);
break;
} }
}$c = new Context();
try{
$download = new DownloadStream( $c);
}catch( Exception $e){
echo "download failure:" . $e->getMessage() . "\n";
}
// 代码中 用 stream 来监视下载过程的,但是没有起作用。程序可以下载并压缩后保存到本地,就是 fopen() 的 $context 参数没有起作用,请问是为什么?
// 命令行下运行程序
// php test.php http://google.com 谢谢了。