<?php
define('IN_IWS', true);
/*****************
//根据自己的需要修改此文档
******************/
require('../includes/cls_mysql.php'); //初始化数据库类
require('../data/config.php'); //数据库配置文件
require('../data/conn.php'); //声明数据库类
/*网站公共函数库*/
require('../includes/lib_common.php');
require('../includes/auto_load.php');
$int_id = $_GET['id'] ? intval($_GET['id']) : 0;
$int_uid = $_GET['id'] ? intval($_GET['id']) : 0;
$str_ip = '127.0.0.1'//userip();
$int_time = time();
//执行sql得到需要下载的文件
//if (!empty($int_id)){
// $arr_info = $DB->fetch_first("SELECT v.*,b.baoti_id,b.title FROM oa_baoti_video v LEFT JOIN oa_baoti b ON b.video=v.id WHERE v.id='$int_id'");
//}
$str_path = $arr_info['savepath'] ? $arr_info['savepath'] : './demo/demo.wmv';
$str_extension = substr(strrchr($str_path, "."),1);
$str_title = $arr_info['title'] ? ($arr_info['title'].'.'.$str_extension) : basename($str_path);
$str_encodedtitle = str_replace("+", "%20", urlencode($str_title));
switch (strtolower($str_extension)) {
case "wmv":
$ctype="video/x-ms-wmv";
break;
default:
$ctype="application/force-download";
}
if (file_exists($str_path)){
$speed = 10240;//此参数为下载最大速度
$file_size = filesize($str_path);
$ranges = getRange($file_size);
$fh = fopen($str_path, "rb");
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
@header("Pragma: public");
@header("Expires: 0");
@header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
@header("Cache-Control: private",false);
@header("Content-Type: video/x-ms-wmv");
@header("Content-Type: $ctype");
//处理中文乱码
if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
@header('Content-Disposition: attachment; filename="'.$str_encodedtitle.'"');
}else if (preg_match("/Firefox/", $_SERVER["HTTP_USER_AGENT"])) {
@header('Content-Disposition: attachment; filename*="utf8\'\''.$str_title.'"');
}else{
@header('Content-Disposition: attachment; filename="'.$str_title .'"');
}
if ($ranges != null) {
@header('HTTP/1.1 206 Partial Content');
@header('Accept-Ranges: bytes');
@header(sprintf('Content-Length: %u',$ranges['end'] - $ranges['start']));
@header(sprintf('Content-Range: bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));
fseek($fh, sprintf('%u',$ranges['start']));
}else{
header("HTTP/1.1 200 OK");
header(sprintf('Content-Length: %s', $file_size));
}
while(!feof($fh)){
echo fread($fh, round($speed*1024, 0));
ob_flush();
sleep(1);
}
($fh != null) && fclose($fh)//插入记录 && $DB->query("INSERT INTO oa_baoti_videodown (video_id, baoti_id, user_id, userip, dtime) VALUES('{$arr_info['id']}', '{$arr_info['baoti_id']}', '$int_uid', '$str_ip', '$int_time')");
//扣积分操作,只扣一次
//$int_downlogid = $DB->result($DB->query("SELECT id FROM oa_baoti_videodown WHERE video_id='{$arr_info['id']}' AND user_id='$int_uid'"), 0);
//if (empty($int_downlogid)){
// updateCredit('download',$int_uid);
//}
}
/** $file_size文件大小 */
function getRange($file_size){
$range = isset($_SERVER['HTTP_RANGE']) ? $_SERVER['HTTP_RANGE'] : null;
if(!empty($range)){
$range = preg_replace('/[\s|,].*/', '', $range);
$range = explode('-',substr($range,6));
if (count($range) < 2 ) {
$range[1] = $file_size;
}
$range = array_combine(array('start','end'),$range);
if (empty($range['start'])) {
$range['start'] = 0;
}
if (!isset ($range['end']) || empty($range['end'])) {
$range['end'] = $file_size;
}
return $range;
}
return null;
}
评论1
最新资源