• 上传文件


    
    
    /* 上传缩略图 */
            $.ajaxFileUpload({
                url:'/admin/Upload/SchoolSendXhb',
                secureuri:false,
                fileElementId:'img_url',
                dataType: 'json',
                data:{filename: 'img_url'},
                success: function (data) {
                    if(typeof(data.err) != 'undefined') {
                        if(data.err != '') {
                            alert(data.err);
                            //$("#img_url_note").html('文件上传失败');
                        } else {
                            //$("#img_url_note").html('文件上传完成');
                            $("#imageurl").attr("src", data.msg.url);
                            $(".img_wenzi").hide();
    
                            // 图片上传(存到数据库)
                            $.ajax({
                                url:'/admin/Index/schoolImgUpload',
                                datatype:'json',
                                data:{school_img:$("#imageurl").attr("src")},
                                success: function(s) {
                                    // 判断code是否定义
                                    if (typeof s.response == 'undefined') {
                                        // 返回信息
                                        alert(s.error_response.msg);
                                    } else {
                                        alert(s.response.msg);
                                    }
                                }
                            });
                        }
                    }
                }
            });
    
    
    
    /**
         * [index 上传文件]
         * @param  string $type        [上传类型]
         * @param  string $upload_path [保存路径]
         * @return [type]              [信息]
         * heyafei 2017/02/06
         */
        public function index($upload_path = '', $type = '')
        {
            header('Content-Type: text/html; charset=UTF-8');
            $inputname = 'filename';//表单文件域name
            $maxattachsize = 1020480000;//最大上传大小100M
            $upext = $type;//上传扩展名
            ini_set('date.timezone','Asia/Shanghai');//时区
    
            if (isset($_SERVER['HTTP_CONTENT_DISPOSITION'])) {//HTML5上传
                if (preg_match('/attachment;s+name="(.+?)";s+filename="(.+?)"/i',
                    $_SERVER['HTTP_CONTENT_DISPOSITION'],$info)) {
                    $_upload_tmp_dir = $this->is_local ? 
                            ini_get("upload_tmp_dir") : '/tmp';
                    $temp_name =  $_upload_tmp_dir .'/'.date("YmdHis").
                                        mt_rand(1000,9999).'.tmp';
    
                    file_put_contents($temp_name, file_get_contents("php://input"));
                    $size = filesize($temp_name);
                    $_FILES[$info[1]]=array('name'=>$info[2],
                                        'tmp_name'=>$temp_name,
                                        'size'=>$size,
                                        'type'=>'',
                                        'error'=>0);
                }
            }
    
            $err = "";
            $msg = "''";
            $upfile=@$_FILES[$inputname];
            if (!isset($upfile)) {
                $err='文件域的name错误';
            } elseif (!empty($upfile['error'])) {
                switch($upfile['error']) {
                    case '1':
                        $err = '文件大小超过了php.ini定义的upload_max_filesize值';
                        break;
                    case '2':
                        $err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
                        break;
                    case '3':
                        $err = '文件上传不完全';
                        break;
                    case '4':
                        $err = '无文件上传';
                        break;
                    case '6':
                        $err = '缺少临时文件夹';
                        break;
                    case '7':
                        $err = '写文件失败';
                        break;
                    case '8':
                        $err = '上传被其它扩展中断';
                        break;
                    case '999':
                    default:
                        $err = '无有效错误代码';
                        break;
                }
            } elseif (empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none') {
                $err = '无文件上传';
            } else {
                $temppath=$upfile['tmp_name'];
                $fileinfo=pathinfo($upfile['name']);
                $extension=$fileinfo['extension'];
                
                
                if($type == 'jpeg,jpg,png'){ //图片
                    $match_res = preg_match('/'.str_replace(',','|',$upext).'/i',$extension);
                }else{ // 附件
                    $match_res = preg_match('/'.str_replace(', ','|',$upext).'/i',$extension);
                }
                //echo $upext.'+++'.$extension;
                //dump(preg_match('/'.str_replace(',','|',$upext).'/i',$extension));die;
                
                if ($match_res) {
                    PHP_VERSION < '4.2.0' && 
                        mt_srand((double)microtime() * 1000000);
                    $filename = date("YmdHis").mt_rand(1000,9999).
                                    '.'.$extension;
                    //同时在web服务器创建备份
                    $attachdir= $upload_path;//上传文件保存路径,结尾不要带/
                    if (!is_dir($attachdir)) {
                        @mkdir($attachdir, 0777);
                    }
                    $bytes=filesize($temppath);
                    if ($bytes > $maxattachsize) {
                        $err = '请不要上传大小超过'.$this -> 
                                formatBytes($maxattachsize).'的文件';
                    } else {
                        $attach_dir = $attachdir;
                        if (!is_dir($attach_dir)) {
                            @mkdir($attach_dir, 0777);
                        }
                        $target = $attach_dir.'/'.$filename;
                        rename($temppath,$target);
                        @chmod($target,0755);
                        $target  = $this->jsonString($target);
                        $pic_dir = $attach_dir.'/'.$filename;
                        if (!file_exists($pic_dir)) {
                            echo "File is not exists";
                            exit;
                        }
                        if (defined('xhb_mode') && xhb_mode) { //晓黑板模式 
                            //$filename = BM("Xhbapi")->imgUpload($pic_dir,$extension,$upfile['name']);
                            
                            $xhbfileinfo = $domainNameArr = array();
                            $xhbfileinfo = BM("Xhbapi")->imgUpload($pic_dir,$extension,$upfile['name']);
                            
                            unlink($pic_dir);
                            //组装图片/附件地址
                            $domainNameArr = BM("Xhbapi")->getdomain();
                            $filename = $domainNameArr['filehost'].$xhbfileinfo['fid'];
                            
                            
                            if($type == 'jpeg,jpg,png'){ //图片
                                
                                $msg = "{'url':'$filename','filename':'$xhbfileinfo[fid]',
                                'returnname':'".$this -> jsonString($filename).
                                "','id':'1'}";
                            }else{
                                $docinfo = $xhbfileinfo['fid'].'(chb)'.$xhbfileinfo['fileName'].'(chb)'.$xhbfileinfo['mimeType'];
                                
                                $msg = "{'url':'$filename','filename':'$xhbfileinfo[fileName]','docinfo':'$docinfo',
                                'returnname':'".$this -> jsonString($filename).
                                "','id':'1'}";
                            }
                            
                            //echo '<pre>';
                            //print_r($xhbfileinfo);die;
                            
                            
                            
                            
                        } else {
                            //获取路由方法
                            $filename = 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.$filename;
                            
                            $msg = "{'url':'$filename',
                            'returnname':'".$this -> jsonString($filename).
                            "','id':'1'}";
                        } 
                        
                        //echo '<pre>';
                        //print_r($filename);die;
                    }
                } else {
                    $err='上传文件扩展名必需为:'.$upext;
                }
                @unlink($temppath);
            }
            return $result= "{'err':'".$this -> jsonString($err)."','msg':".$msg."}";
        }
  • 相关阅读:
    校园网络安全CTF 第一题 和 你真了解我吗?
    href="#" 链接到当前页面
    Redis的Set无序集合命令
    Redis的List链表类型命令
    Redis的String、Hash类型命令
    redis2.8.xx安装配置
    ZendFramework安装配置
    复选框的全选、反选
    列表中被点击的行加亮背景色
    SQL中的替换函数replace()使用
  • 原文地址:https://www.cnblogs.com/heyafei/p/6518986.html
Copyright © 2020-2023  润新知