php隐藏下载地址

download($url,$file_name);

function download($url, $filename) {   
    // 获得文件大小, 防止超过2G的文件, 用sprintf来读
//print "文件地址:".$url."<br>";
//print "文件大小:".filesize ($url)."<br>";
    $filesize = sprintf ( "%u", getFileSize ( $url ) );
//print $filesize;   
    if (! $filesize) {
   //print "找不到文件!";
        //return;   
     }   
     header ( "Content-type:application/octet-stream\n" ); //application/octet-stream   
     header ( "Content-type:unknown/unknown;" );   
     header ( "Content-disposition: attachment; filename=\"" . $filename . "\"" );   
     header ( 'Content-transfer-encoding: binary' );   
    if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候,采用206的断点续传头   
        $range = explode ( '=', $range );   
        $range = $range [1];   
         header ( "HTTP/1.1 206 Partial Content" );   
         header ( "Date: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );   
         header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", filemtime ( $url ) ) . " GMT" );   
         header ( "Accept-Ranges: bytes" );   
         header ( "Content-Length:" . ($filesize - $range) );   
         header ( "Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize );   
         header ( "Connection: close" . "\n\n" );   
     } else {   
         header ( "Content-Length:" . $filesize . "\n\n" );   
        $range = 0;   
     }   
     loadFile ( $url );   
}   
function loadFile($filename, $retbytes = true) {   
    $buffer = '';   
    $cnt = 0;   
    $handle = fopen ( $filename, 'rb' );   
    if ($handle === false) {   
        return false;   
     }   
    while ( ! feof ( $handle ) ) {   
        $buffer = fread ( $handle, 1024 * 1024 );   
        echo $buffer;   
         ob_flush ();   
        flush ();   
        if ($retbytes) {   
            $cnt += strlen ( $buffer );   
         }   
     }   
    $status = fclose ( $handle );   
    if ($retbytes && $status) {   
        return $cnt; // return num. bytes delivered like readfile() does.   
     }   
    return $status;   
}

//读取文件大小
function getFileSize($url){
$url = parse_url($url);
if($fp = fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){
   fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
   fputs($fp,"Host:$url[host]\r\n\r\n");
   while(!feof($fp)){
    $tmp = fgets($fp);
    if(trim($tmp) == ''){
     break;
    }elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){
     return trim($arr[1]);
    }
   }
   return null;
}else{
   return null;
}
}

posted @ 2012-08-15 15:12  phpbin  阅读(675)  评论(0编辑  收藏  举报