首页 > 代码库 > 七牛存储图片代码
七牛存储图片代码
1 //上传到七牛 2 //单个文件 3 //formname: 表单名字; pre: 图片Url中显示的图片名字(也就是七牛中的key) 4 public function upImage($formname, $pre) 5 { 6 if (empty($_FILES[$formname][‘size‘])) { 7 return array( 8 ‘qiniu_name‘ => ‘000‘, 9 ‘file_name‘ => ‘0000‘ 10 ); 11 } 12 13 //取得后缀 14 $realname = $_FILES[$formname][‘name‘]; 15 $arrRealName = explode(‘.‘, $realname); // 推荐: $arrFileInfo = pathinfo($realname); $file_name = $arrFileInfo[‘filename‘];$extension = $arrFileInfo[‘extension‘];... 16 $file_name = $arrRealName[0]; 17 $stuffix = end($arrRealName); 18 19 //组织完整入库文件名 20 $rand_time = $this->getMsec(); 21 $qiniu_name = ‘qiniu‘.$rand_time.‘_‘.$pre.‘.‘.$stuffix; 22 $filepath = $_FILES[$formname][‘tmp_name‘]; 23 24 //上传 25 $this->CI->load->library(‘qiniu‘); 26 $this->CI->qiniu->upFile($qiniu_name, $filepath); 27 // $imgbaseurl = $this->CI->qiniu->getImageBaseUrl($filename); 28 // return $imgbaseurl; 29 30 return array( 31 ‘qiniu_name‘ => $qiniu_name, 32 ‘file_name‘ => $file_name 33 ); 34 } 35 //上传多个图片到七牛 36 public function upMoreImage($formname, $pre) 37 { 38 if (empty($_FILES[$formname])) { 39 return ‘‘; 40 } 41 42 $count = count($_FILES[$formname][‘name‘]); 43 44 $arrImages = array(); 45 foreach ($_FILES[$formname][‘name‘] as $k => $name) { 46 if ($_FILES[$formname][‘size‘][$k]) { 47 $arrImages[$k][‘name‘] = $name; 48 $arrImages[$k][‘type‘] = $_FILES[$formname][‘type‘][$k]; 49 $arrImages[$k][‘tmp_name‘] = $_FILES[$formname][‘tmp_name‘][$k]; 50 $arrImages[$k][‘error‘] = $_FILES[$formname][‘error‘][$k]; 51 $arrImages[$k][‘size‘] = $_FILES[$formname][‘size‘][$k]; 52 } 53 } 54 55 $this->CI->load->library(‘qiniu‘); 56 $arrUpInfo = array(); 57 foreach ($arrImages as $k => $v) { 58 //取得后缀 59 $realname = $v[‘name‘]; 60 $arrRealName = explode(‘.‘, $realname); 61 $file_name = $arrRealName[0]; 62 $stuffix = end($arrRealName); 63 64 //组织完整入库文件名 65 $rand_time = $this->getMsec(); 66 $qiniu_name = ‘qiniu‘.$rand_time.‘_‘.$pre.‘.‘.$stuffix; 67 $filepath = $v[‘tmp_name‘]; 68 69 //上传 70 $this->CI->qiniu->upFile($qiniu_name, $filepath); 71 72 $arrUpInfo[$k] = array( 73 ‘qiniu_name‘ => $qiniu_name, 74 ‘file_name‘ => $file_name 75 ); 76 } 77 78 return $arrUpInfo; 79 80 }
七牛存储图片代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。