/** * 上传头像 * */ public function actionUploadheadimg() { $parameters = $this->getFormParameters(); $loginToken = $parameters['loginToken']; $imgData = $_FILES['avatarImageFile']; $image_prefix = 'http://7xntis.com1.z0.glb.clouddn.com'; if (empty($loginToken)) { $this->ApiReturnJson(550, 'token无效', array()); } $AccountModel = new AccountModel(); $userId = $AccountModel->getUserIdByToken($loginToken); if (empty($userId) || intval($userId) <= 0) { $this->ApiReturnJson(551, 'token无效', array()); } $dir = 'upload/' . date('Y') . '/' . date('m') . '/'; $path = $dir . md5(time() . $userId) . '.jpeg'; if (!is_dir($dir)) { if (!mkdir($dir, 0777, true)) { $this->ApiReturnJson(560, '创建目录失败', array()); } } if (!is_writable($dir)) { $this->ApiReturnJson(561, '系统不可写', array()); } if (!move_uploaded_file($imgData['tmp_name'], $path)) { $this->ApiReturnJson(562, '文件写入失败', array()); } /** * 上传到七牛 * */ $uploadRet = uploadImg::getInstance()->uploadImag($path, $dir); if ($uploadRet['code'] == true) { //URL写入数据库 $where['id'] = $userId; $data['head_image_url'] = $image_prefix . $uploadRet['content']['key']; $ret = $AccountModel->updateUserInfo($where, $data); if ($ret == true) { $this->ApiReturnJson(200, '图片上传成功', array('ret' => $data['head_image_url'])); } else { $this->ApiReturnJson(570, '图片上传七牛失败', array()); } } else { $this->ApiReturnJson(570, '图片上传七牛失败', array()); } }