| 
 | 
 
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();
            }
            
        }
        
    }
?>  复制代码 |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |