php base64各种上传

<?php
/* 
 *  Bpm接口 所需要的处理方法
 */
trait Bpm_trait{
    
    /* 处理base64文件 */
    public function create_base64_file($base64_file, $up_dir){
        header("content-type:text/html;charset=utf-8");
        $base64_img = trim($base64_file);
        
        if(!file_exists($up_dir)){
            mkdir($up_dir,0777);
        }
         
        // if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
        if(preg_match('/^(data:\s*(image|application)\/(\w+);base64,)/', $base64_img, $result)){
            $type = $result[3];
            if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png','pdf'))){
                // $new_file = $up_dir.date('YmdHis_').mt_rand().'.'.$type;
                $fileName = date('YmdHis_').mt_rand().'.'.$type;
                $new_file = $up_dir.$fileName;                     
                if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
                    // $img_path = $_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]."/server/api/".str_replace('./', '', $new_file);
                    $img_path = $_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]."/server/api/subscriber_upload_file/".$fileName;
                    
                    return $img_path;
                }else{
                   $this->db->pdo->rollBack();
                   throw new Exception("图片上传失败");
                }
            }else{
                $this->db->pdo->rollBack();
                throw new Exception("图片上传类型错误");
            }         
        }else{
           $this->db->pdo->rollBack();
           throw new Exception("图片上传错误");
        }        
    } 

    
    /* 上传订单合同 */
    public function upload_orderContract_file($base64_file, $up_dir){
        header("content-type:text/html;charset=utf-8");
        $base64_img = trim($base64_file);
        
        if(!file_exists($up_dir)){
            mkdir($up_dir,0777);
        }

         $type_arr = array(
                'msword' => 'doc',
                'vnd.openxmlformats-officedocument.wordprocessingml.document'   => 'docx',
                'vnd.ms-excel'                                                  => 'xls',  
                'vnd.openxmlformats-officedocument.spreadsheetml.sheet'         => 'xlsx', 
                'pdf'                                                           => 'pdf',  
                'vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx', 
                'vnd.ms-powerpoint'                                             => 'ppt',  
         );

        if(preg_match('/^(data:\s*application\/([^;]+);base64,)/', $base64_img, $result)){
            $type = $result[2];
            if(in_array($type,array('msword', 'vnd.openxmlformats-officedocument.wordprocessingml.document', 'pdf', 'pptx', 'ppt'))){
                $fileName = date('YmdHis_').mt_rand().'.'.$type_arr[$type];
                $new_file = $up_dir.$fileName;       
                if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
                    // $file_path = $_SERVER["HTTP_HOST"]."/server/api/".str_replace('./', '', $new_file);
                    $file_path = $_SERVER["HTTP_HOST"]."/server/api/orderContract_upload_file/".$fileName;                    
                    return $file_path;
                }else{
                   $this->db->pdo->rollBack();
                   throw new Exception("合同文件上传失败");
                }
            }else{
                $this->db->pdo->rollBack();
                throw new Exception("合同文件上传类型错误");
            }         
        }else{
           $this->db->pdo->rollBack();
           throw new Exception("合同文件上传错误");
        }        
    }
       
    /* curl post 请求 */
    public function curl_post($url,$data,$headers){        
         // $headers = array(    
            // 'Cookie: JSESSIONID=A02439BE65581E57C287BF798111FFAB'            
        // );    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0); //设置超时
        if(0 === strpos(strtolower($url), 'https')) {
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        $output = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
        curl_close($ch);
        return $output;
    }
    
    /**
     * 求两个日期之间相差的天数
     * (针对1970年1月1日之后,求之前可以采用泰勒公式)
     * @param string $day1
     * @param string $day2
     * @return number
     */
    public function diffBetweenTwoDays($day1, $day2){
      $second1 = strtotime($day1);
      $second2 = strtotime($day2);
        
      if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
      }
      $day = (($second1 - $second2) / 86400)+1;
      return $day;
    }
    
}

 

posted @ 2020-05-22 00:39  没事就更  阅读(767)  评论(0编辑  收藏  举报