/** * 上传头像 * */ 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()); } }
//$obj->set_debug_mode(FALSE); //设置开启三级域名,三级域名需要注意,域名不支持一些特殊符号,所以在创建bucket的时候若想使用三级域名,最好不要使用特殊字符 $obj->set_enable_domain_style(TRUE); } $bucket = QRIMG_B; $folder = 'headimg/'; $object = $folder . $imgName; if ($save_type == 'url') { $content = file_get_contents($content); } else { $content = $content; } $upload_file_options = array('content' => $content, 'length' => strlen($content), ALIOSS::OSS_HEADERS => array('Expires' => date('Y-m-d H:i:s', time() + 3600 * 24 * 10))); $response = $obj->upload_file_by_content($bucket, $object, $upload_file_options); //echo 'upload file {'.$object.'}'.($response->isOk()?'ok':'fail')."\n"; //_format($response); return HTTP_IMG_HOST . $object; } } /** * demo * @var uploadImg */ $upload_app = new uploadImg(); $filePath = dirname(__FILE__) . '/'; $imgName = "test.jpg"; $fileImgPath = $filePath . $imgName; $ossImgUrl = $upload_app->upload_by_content('', $imgName, file_get_contents($fileImgPath), 'openFileImg'); echo $ossImgUrl; ?>