return_json('请选择图片'); } if (!$file['tmp_name']) {//太大的图片上传,这个参数会变成空的 $this->return_json('参数错误'); } if (!file_exists(TEMP_PATH)) { $oldumask = umask(0); mkdir(TEMP_PATH, 0777, true); umask($oldumask); } $tmp = TEMP_PATH . md5($file['name'] . uniqid()) . substr($file['name'], strpos($file['name'], '.', strlen($file['name']) - 1)); move_uploaded_file($file['tmp_name'], $tmp); if (!filesize($tmp)) { $this->return_json('图片有点问题,换个小的试试'); } //上传图片到FTP $res = $this->upload_img_qiniu($tmp, "market/"); if (!$res) { $this->return_json('上传失败'); } $data['full_url'] = build_qiniu_image_url($res['photo']); $data['url'] = $res['photo']; $this->return_response($data); } /** * @param string $file 上传的文件 * @param string $path 要保存的目录 * @param string $filename 原始文件名称 * @return array * @throws Exception */ private function upload_img_qiniu($file, $path = '', $filename = '') { $phoId = md5(uniqid() . mt_rand(0, 10000) . time()); $filename = $filename ? $filename : $file; $ext_arr = explode(".", $filename); $ext = count($ext_arr) > 1 ? $ext_arr[count($ext_arr) - 1] : 'jpg'; if (is_uploaded_file($file)) { //上传图片 $oriPath = TEMP_PATH . '/p_' . $phoId . '_ori.' . $ext; move_uploaded_file($file, $oriPath); } else { $oriPath = $file; } $oriKey = 'p_' . $phoId . '.' . $ext; // 上传到七牛后保存的文件名 $photo = $path . date('Ym') . "/" . $oriKey; //上传图片到FTP $this->load->library('qiniu'); $res = $this->qiniu->save($photo, file_get_contents($oriPath)); $img_size = getimagesize($oriPath); $file_size = filesize($oriPath); $size = "{$img_size[0]},{$img_size[1]},{$file_size}"; unlink($oriPath); if ($res) { $size = getimagesize($res['url']); return array('photo' => $res['file'], 'size' => $size); } else { return array(); } } }