用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4197|回复: 3
收起左侧

[库 Library] 分享我的下载类

[复制链接]
发表于 2009-4-11 10:37:14 | 显示全部楼层 |阅读模式
1.首先,将Down.php放到system/application/libraries文件夹,这个不用多说。
2.加载之后,在要用到的Controller中:
PHP复制代码
function downme($file_name){//下载文件
            $file_name = $file_name.".rar";
            $base=base_url();
            $file_dir =$base.'download/';
            $realpath=$file_dir.$file_name;
            $this->down->downme($realpath,'http');
        }
复制代码

附注:希望大家完善这个类,实现更多的功能。谢谢。
Down.php

PHP复制代码
<?php
    class Down {    
        function downme($filepathh,$type='relative') {//$filepathh传入完整路径,如./download/a.rar
           
            if($type=='relative'){//如果是相对路径的下载
//                ob_start();
                if(!file_exists($filepathh)){//如果没有此文件
                    echo "文件找不到";
                }
                else{//如果存在文件
                    $file=fopen($filepathh,"r");
                    Header("Content-type: application/octet-stream");
                    Header("Accept-Ranges: bytes");
                    Header("Accept-Length: ".filesize($filepathh));
                    Header("Content-Disposition: attachment; filename=" . $file_name);
                    // 输出文件内容
                    echo fread($file,filesize($file_dir . $file_name));
                    fclose($file);
                    exit;
                }
//                ob_end_clean();
            }
            else{
                if($type=='http'){//如果是http,ftp形式的下载,CI中适用,因为要用到base_url()
//                    ob_start();
                    $file=fopen($filepathh,"r");
                    if(!$file){//如果文件找不到
                        echo "文件找不到";
                    }
                    else{//如果存在此文件
                        $file_name=basename($filepathh);
                        Header("Content-type: application/octet-stream");
                        Header("Content-Disposition: attachment; filename=" . $file_name);
                        while (!feof ($file)) {
                            echo fread($file,50000);
                        }
                        fclose ($file);
                    }
                   
                }
//                ob_end_clean();
            }
           
        }
       
    }
?>
复制代码

Down.rar

690 Bytes, 下载次数: 78

评分

参与人数 1威望 +5 收起 理由
Hex + 5 原创内容

查看全部评分

发表于 2009-4-11 12:19:03 | 显示全部楼层
好东西,加分~
 楼主| 发表于 2009-4-12 12:07:32 | 显示全部楼层
非常感谢老大,first time!
发表于 2011-3-29 10:23:02 | 显示全部楼层
确实是好东西

本版积分规则