首页 > 代码库 > 七牛存储图片代码

七牛存储图片代码

 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     }

七牛存储图片代码