//上传附件
        public function up2(){
        $this->Csadmin->Admin_Login();
            $nums=intval($this->input->get('nums')); //支持数量
            $types=$this->input->get('type',true);  //支持格式
        $fhid = $this->input->get('fhid',true); //返回ID参数
        $dir = $this->input->get('dir',true);   //上传目录
                
        $fdzurl = $this->input->get('fdzurl',true);   //get方式获取原链接   歌曲编辑页面要通过判断sid=2 进入到该上传页面   <div class="layui-input-inline" style="width: auto;"><a href="javascript:cscms.get_open(\''.site_url('upload/up2').'?fhid=purl&dir=music&type=*.mp3;*.m4a;*.mp4;*.wma&fdzurl='.$purl.'\',\'上传歌曲\',\'500px\',\'360px\')" class="layui-btn layui-btn-primary"><i class="layui-icon" style="color:green"></i>上传歌曲</a></div>
                
        $data['fhid']=(empty($fhid))?"pic":$fhid;
        $data['sid']=intval($this->input->get('sid')); //返回输入框方法,0替换、1换行增加
        $data['fid']=$this->input->get('fid',true);   //返回ID,一个页面多个返回可以用到
                
                //重新定义的上传地址 
        $data['upsave']=site_url('upload/up_save2?fdzurl=').$fdzurl;
                
        $data['size'] = UP_Size.'kb';
        $data['types'] =(empty($types))?"gif,png,jpg,jpeg":str_replace(array(';*.',';','*.'),array(',','',''),$types);
        $data['nums']=($nums==0)?1:$nums;
                if($data['fid']==='undefined') $data['fid']='';
                $str['id']=$_SESSION['admin_id'];
                $str['name']=$_SESSION['admin_name'];
                $str['pass']=$_SESSION['admin_pass'];
        $key = sys_auth(addslashes(serialize($str)),'E');
        $params = array();
                $this->load->library('csup');
        if(UP_Mode == 3){ //七牛
                $token = $this->csup->qiniu_uptoken();
                $params['token'] = $token;
                $data['dir'] = date('Ymd').'/';
                $data['upsave'] = is_ssl().'upload.qiniu.com/';
        }elseif(UP_Mode == 4){ //阿里云OSS
                $params = $this->csup->osssign();
                $data['dir'] = date('Ymd').'/';
                $data['upsave'] = $params['host'];
        }else{ //本地
                $data['dir'] = $dir;
                $params['dir'] = $dir;
                $params['upkey'] = $key;
        }
        $data['params'] = json_encode($params);
                $data['fhhost'] = '';
                if(UP_Mode>1 && ($dir=='music' || $dir=='video')){
                        $fhhost = $this->csup->down(UP_Mode);
                        if(substr($fhhost,-1) != '/') $fhhost .= '/';
                        $data['fhhost'] = $fhhost;
                }
        $this->load->view('upload2.html',$data);   //复制 upload.html   该为   upload2.html    可改可不改
        }

    //保存附件
        public function up_save2(){
        $key=$this->input->post('upkey',true);
        $this->Csadmin->Admin_Login($key);
        $dir=$this->input->post('dir',true);
                
                //获取过来的原始地址
        $fdzurl=$this->input->get('fdzurl',true);
                
                //拆分路径和文件名
                $ywjmc=basename($fdzurl,'.'.$fdzurl['extension']);
                $ywjml=str_replace($ywjmc,'',$fdzurl);
                
                //重组实际物理地址
                $xpath=FCPATH.$fdzurl;
                
                //var_dump($ywjml.'    <bR>'.$ywjmc) ;exit();
                if(empty($dir) || !preg_match('/^[0-9a-zA-Z\_]*$/', $dir)) {  
            $dir='other';
                }
                //上传目录
                if(UP_Mode==1 && UP_Pan!=''){
                    $path = UP_Pan.'/attachment/'.$dir.'/'.date('Ym').'/'.date('d').'/';
                        $path = str_replace("//","/",$path);
                }else{
                    $path = FCPATH.'attachment/'.$dir.'/'.date('Ym').'/'.date('d').'/';
                }
                if (!is_dir($path)) {
            mkdirss($path);
        }
                $tempFile = $_FILES['file']['tmp_name'];
                $file_name = $_FILES['file']['name'];
                $file_size = filesize($tempFile);
        $file_ext = strtolower(trim(substr(strrchr($file_name, '.'), 1)));
        $file_type = $_FILES['file']['type'];

        //判断文件MIME类型
        if($file_type != 'application/octet-stream'){
                        $mimes = get_mimes();
                        if(!is_array($mimes[$file_ext])) $mimes[$file_ext] = array($mimes[$file_ext]);
                        if(isset($mimes[$file_ext]) && $file_type !== false && !in_array($file_type,$mimes[$file_ext],true)){
                                getjson(L('plub_04'),1,1);
                        }
                }

        //检查扩展名
                $ext_arr = explode("|", UP_Type);
        if(!in_array($file_ext,$ext_arr,true)){
            getjson(L('plub_04'),1,1);
                }elseif(in_array($file_ext, array('gif', 'jpg', 'jpeg', 'jpe', 'png'), TRUE) && @getimagesize($tempFile) === FALSE){
            getjson(L('plub_05'),1,1);
                }
        //PHP上传失败
        if (!empty($_FILES['file']['error'])) {
            switch($_FILES['file']['error']){
                    case '1':$error = L('plub_06');break;
                    case '2':$error = L('plub_07');break;
                    case '3':$error = L('plub_08');break;
                    case '4':$error = L('plub_09');break;
                    case '6':$error = L('plub_10');break;
                    case '7':$error = L('plub_11');break;
                    case '8':$error = 'File upload stopped by extension。';break;
                    case '999':default:$error = L('plub_12');
            }
            getjson($error,1,1);
        }
        //新文件名
                $file_name=random_string('alnum', 20). '.' . $file_ext;
                $file_path=$path.$file_name;
                
                
                //var_dump($tempFile.$file_name.$file_size.$file_ext.$file_type.$fdzurl.$tempFile.$file_path.$path.FCPATH);exit();
                if (move_uploaded_file($tempFile, $xpath) !== false) { //上传成功  改为新的重组地址

                $filepath=(UP_Mode==1)?'/'.date('Ym').'/'.date('d').'/'.$file_name : '/'.date('Ymd').'/'.$file_name;

                //判断水印
                if($dir!='links' && CS_WaterMark==1){
                                        if($file_ext=='jpg' || $file_ext=='png' || $file_ext=='gif' || $file_ext=='bmp' || $file_ext=='jpge'){
                             $this->load->library('watermark');
                         $this->watermark->imagewatermark($xpath);  //改为新的重组地址
                                        }
                }

                                //判断上传方式
                $this->load->library('csup');
                                $res=$this->csup->up($xpath,$file_name);   //改为新的重组地址
                                if($res){
                                        if($dir=='music' || $dir=='video'){
                                                if(UP_Mode==1){
                                                    $filepath = 'attachment/'.$dir.$filepath;
                                                }else{
                                                        $filepath = annexlink($filepath);
                                                }
                                        }
                                        getjson(array('msg'=>'ok','fileurl'=>$fdzurl),1,1);   //json数据该为原始连接
                                }else{
                                        @unlink($xpath);  //改为新的重组地址
                    getjson('no',1,1);
                                }

                }else{ //上传失败
                          getjson('no',1,1);
                }
        }


评论 (0)

请登录

嘿,我来帮您