3.2.1自动获取上传配置上传

jerry thinkphp 2015年11月19日 收藏
3.2.1我发现有又拍云的上传驱动,所以把上传函数改造了一下以便于一次配置后可以自动判断上传
  1. /*
  2. 自动获取上传配置上传函数
  3. */
  4. function filed($config='',$path=''){
  5.     $Upload_type=C('upload_type');
  6.     $upload_driver=C($Upload_type);
  7.     if (!$config) {
  8.         $config = array(
  9.         'maxSize'    =>    3145728,
  10.         'rootPath'   =>    'Upload',
  11.         'savePath'   =>    $path,
  12.         'saveName'   =>    'time',
  13.         'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
  14.         'autoSub'    =>    true,
  15.         'subName'    =>    array('date','Ymd'),
  16.         'replace'    =>     true,
  17.         );
  18.     }
  19.     // 实例化上传类
  20.     $upload = new \Think\Upload($config,$Upload_type,$upload_driver);
  21.     $path?true:$upload->savePath='/';
  22.     $info   =   $upload->upload();          // 上传文件      
  23.     if(!$info) {    
  24.         $data['type']=0;
  25.         $data['msg']=$upload->getError();  
  26.         return $data;   // 上传错误提示错误信息  
  27.     }else{
  28.         $request=array();
  29.         foreach ($info as $key => $value) {
  30.             $request[]=$value;
  31.         }
  32.         if ($Upload_type!=='Local') {
  33.             for($i=0;$i<count($request);$i++){
  34.             $res[]='http://'.C('files_domain').'/'.$config['rootPath'].$request[$i]['savepath'].$request[$i]['savename'];
  35.             }
  36.         }else{
  37.             for($i=0;$i<count($request);$i++){
  38.             $res[]=__ROOT__.'/'.$config['rootPath'].$request[$i]['savepath'].$request[$i]['savename'];
  39.             }
  40.         }
  41.         $data['type']=1;
  42.         $data['msg']=$res;
  43.         return $data;
  44.     }
  45. }
配置项增加
  1. <?php return array(
  2.     'files_domain'=>'文件空间绑定域名',
  3.     'upload_type'=>'Upyun',//或Local
  4.     'upyun'=>array(
  5.         'bucket'=>'空间名',
  6.         'username'=>'用户名',
  7.         'password'=>'密码',
  8.         'host'=>'v0.api.upyun.com'
  9.         ),
  10.     );
  11. ?>