致力于网页设计制作教程:HTTP://WWW.ASP119.COM

当前位置:首页 > 网页制作 > CMS技巧

使用帝国制作txt文本内容下载的方法

作者:迷失的宇 来源:ASP急救中心 浏览: 添加日期:2011-02-11  我要评论( )

[导读]大家估计都遇到过,在制作小说站的时候,要提供的txt文本下载,这样既可以节省空间,又对用户浏览提供了方便,但是在帝国中如果才能实现这种方法,能否实现,下面我们具体看看作者是如果实现的。

大家估计都遇到过,在制作小说站的时候,要提供的txt文本下载,这样既可以节省空间,又对用户浏览提供了方便,但是在帝国中如果才能实现这种方法,能否实现,下面我们具体看看作者是如果实现的。

1.看下面粘贴的帝国原函数DoTypeForDownurl() Header 与 Meta 两种方式都使用的真实地址,在这两种方式下浏览器不能读取的文件格式,例如RAR,ZIP等将直接直接弹出一个真实地址下载,txt等浏览器可读的文件格式则直接在线浏览;

2.默认不选择前缀的情况下,使用是Header

3.READ 这种方式下是利用PHP读取文件,并传递给浏览器,无论 RAR 还是TXT都会弹出下载,而不是浏览器直接读取,弹出的地址也是加密的地址类似:xxx.com/enews=DownSoft&classid=39&id=11&pathid=0&pass=7b9516932a88d44b4e4610e209f6288d&p=22:::J2qCrBGqmrxK

4.READ的加密地址判断是否有下载权限,也就是地址是否正确是由e/class/DownSysFun.php中DownSoft()函数来判断的,经测试如果不更改后台->系统设置->模型设置->下载验证码 的情况下流传出去是可以持续被下载的(没有用户组,积分等判断限制 如果要避免那么e/class/DownSysFun.php 第一行 $DownSys_CheckIp=0; 中的0改为1),帝国CMS也是通过这个方式来防盗链;

5.如果我不想使用下载地址前缀,而让TXT等格式可以弹出下载,那么你可以在e/class/DownSysFun.php中搜索DoTypeForDownurl($downurl,$downurlr['downtype']);上一行加入如下代码:
 

//trylife add check type 20101231  
//不使用下载地址前缀 让txt等浏览器可读类型文件弹出下载  
//A.F4  
$type2r=array(".xls",".txt",);  
if(in_array(GetFiletype($downurl),$type2r))  
{  
    $downurlr['downtype']=2;  

测试版本:帝国CMS6.5
功能位置:后台->其他管理->下载模型管理->管理地址前缀
相关函数:e/class/DownSysFun.php DoTypeForDownurl() , QDownLoadFile() , DownSoft()
功能解释:
HEADER:使用header转向,通常设为这个。
META:直接转自,如果是FTP地址推荐选择这个。
READ:使用PHP程序读取,防盗链较强,但较占资源,服务器本地小文件可选择。

具体用到的代码如下:

//ecms e/class/DownSysFun.php  
function DownSoft($classid,$id,$pathid,$p,$pass){  
    global $empire,$public_r,$level_r,$class_r,$emod_r,$user_tablename,$user_userid,$user_rnd,$user_group,$user_userfen,$user_userdate,$user_username,$dbtbpre;  
    $id=(int)$id;  
    $classid=(int)$classid;  
    $pathid=(int)$pathid;  
    if(emptyempty($id)||emptyempty($p)||emptyempty($classid))  
    {  
        printerror("ErrorUrl","history.go(-1)",1);  
    }  
    $p=RepPostVar($p);  
    $p_r=explode(":::",$p);  
    $userid=$p_r[0];  
    $rnd=$p_r[1];  
    //验证码  
    $cpass=md5(ReturnDownSysCheckIp()."wm_chief".$public_r[downpass].$userid);  
    if($cpass<>$pass)  
    {  
        printerror("FailDownpass","history.go(-1)",1);  
    }  
    //表不存在  
    if(emptyempty($class_r[$classid][tbname]))  
    {  
        printerror("ExiestSoftid","history.go(-1)",1);  
    }  
    $mid=$class_r[$classid][modid];  
    $tbname=$class_r[$classid][tbname];  
    $ok=1;  
    $r=$empire->fetch1("select * from {$dbtbpre}ecms_".$tbname." where id='$id' and classid='$classid'");  
    if(emptyempty($r[id]))  
    {  
        printerror("ExiestSoftid","history.go(-1)",1);  
    }  
    //副表  
    if($emod_r[$mid]['tbdataf']&&$emod_r[$mid]['tbdataf']<>',')  
    {  
        $selectdataf=substr($emod_r[$mid]['tbdataf'],1,-1);  
        $finfor=$empire->fetch1("select ".$selectdataf." from {$dbtbpre}ecms_".$tbname."_data_".$r[stb]." where id='$r[id]'");  
        $r=array_merge($r,$finfor);  
    }  
    //区分下载地址  
    $path_r=explode("\r\n",$r[downpath]);  
    if(!$path_r[$pathid])  
    {  
        printerror("ExiestSoftid","history.go(-1)",1);  
    }  
    $showdown_r=explode("::::::",$path_r[$pathid]);  
    $downgroup=$showdown_r[2];  
    //下载权限  
    if($downgroup)  
    {  
        $userid=(int)$userid;  
        //取得会员资料  
        $u=$empire->fetch1("select * from ".$user_tablename." where ".$user_userid."='$userid' and ".$user_rnd."='$rnd'");  
        if(emptyempty($u[$user_userid]))  
        {printerror("MustSingleUser","history.go(-1)",1);}  
        //下载次数限制  
        $setuserday="";  
        if($level_r[$u[$user_group]][daydown])  
        {  
            $setuserday=DoCheckMDownNum($userid,$u[$user_group]);  
        }  
        if($level_r[$downgroup][level]>$level_r[$u[$user_group]][level])  
        {  
            printerror("NotDownLevel","history.go(-1)",1);  
        }  
        //点数是否足够  
        $showdown_r[3]=intval($showdown_r[3]);  
        if($showdown_r[3])  
        {  
            //---------是否有历史记录  
            $bakr=$empire->fetch1("select id,truetime from {$dbtbpre}enewsdownrecord where id='$id' and classid='$classid' and userid='$userid' and pathid='$pathid' and online=0 order by truetime desc limit 1");  
            if($bakr[id]&&(time()-$bakr[truetime]<=$public_r[redodown]*3600))  
            {}  
            else 
            {  
                //包月卡  
                if($u[$user_userdate]-time()>0)  
                {}  
                //点数  
                else 
                {  
                    if($showdown_r[3]>$u[$user_userfen])  
                    {  
                        printerror("NotEnoughFen","history.go(-1)",1);  
                    }  
                    //去除点数  
                    $usql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."-".$showdown_r[3]." where ".$user_userid."='$userid'");  
                }  
                //备份下载记录  
                $utfusername=doUtfAndGbk($u[$user_username],1);  
                BakDown($classid,$id,$pathid,$userid,$utfusername,$r[title],$showdown_r[3],0);  
            }  
        }  
        //更新用户下载次数  
        if($setuserday)  
        {  
            $usql=$empire->query($setuserday);  
        }  
    }  
    //总下载数据增一  
    $usql=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid][tbname]." set totaldown=totaldown+1 where id='$id'");  
    $downurl=stripSlashes($showdown_r[1]);  
    $downurlr=ReturnDownQzPath($downurl,$showdown_r[4]);  
    $downurl=$downurlr['repath'];  
    //防盗链  
    @include(ECMS_PATH."e/class/enpath.php");  
    $downurl=DoEnDownpath($downurl);  
    db_close();  
    $empire=null;  
 
    //trylife add check type 20101231  
    //不使用下载地址前缀 让txt等浏览器可读类型文件弹出下载  
    //A.F4  
    $type2r=array(".doc",".xls",".docx",".ppt",".pptx",".txt","pdf",);  
    if(in_array(GetFiletype($downurl),$type2r))  
    {  
        $downurlr['downtype']=2;  
    }  
    //trylife add check type 20101231 [e]  
 
    DoTypeForDownurl($downurl,$downurlr['downtype']);  
}  
//下载操作  
function DoTypeForDownurl($downurl,$type=0){  
    global $public_r;  
 
    if($type==1)//meta  
    {  
        echo"<META content='\"0;url=$downurl\"' http-equiv='\"refresh\"'>";  
    }  
    elseif($type==2)//read  
    {  
        QDownLoadFile($downurl);  
    }  
    else//header  
    {  
        Header("Location:$downurl");  
    }  
    exit();  
}  
 
//下载  
function QDownLoadFile($file){  
    global $public_r;  
    if(strstr($file,"\\")) 
    { 
        $exp="\\"; 
    } 
    elseif(strstr($file,"/")) 
    { 
        $exp="/"; 
    } 
    else 
    { 
        Header("Location:$file"); 
        exit(); 
    } 
    if(strstr($file,$exp."e".$exp)||strstr($file,"..")||strstr($file,"?")||strstr($file,"#")) 
    { 
        Header("Location:$file"); 
        exit(); 
    } 
    if(strstr($file,$public_r[fileurl])) 
    { 
        $file=str_replace($public_r[fileurl],'/d/file/',$file); 
    } 
    if(!strstr($file,"://"))  
    {  
        if(!file_exists($file))  
        {  
            $file="../..".$file; 
        } 
    } 
    $filename=GetDownurlFilename($file,$exp); 
    if(empty($filename)) 
    { 
        Header("Location:$file"); 
        exit(); 
    } 
    //下载 
    Header("Content-type: application/octet-stream"); 
    //Header("Accept-Ranges: bytes"); 
    //Header("Accept-Length: ".$filesize); 
    Header("Content-Disposition: attachment; filename=".$filename);  
    echo ReadFiletext($file);  

Tags:

相关文章

文章评论

正在载入评论数据中...
用户名:
  QQ号:
*
  匿名发表
验证码: