1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- /**
- * 前端上传接口
- * @ApiWeigh (90)
- */
- class Upload extends Api
- {
- protected $noNeedLogin = '';//都需要登录
- /**
- * 上传头像
- *
- * @ApiTitle (上传头像)
- * @ApiSummary (上传头像上传头像)
- * @ApiMethod (POST)
- * @ApiHeaders (name="user-token", type="string", required=true, description="请求的Token")
- * @ApiRoute (/api/upload/upload)
- * @ApiParams (name="user_id", type="int", required=true, description="用户user_id")
- * @ApiParams (name="token", type="int", required=true, description="请求的Token")
- * @ApiParams (name="file", type="file", required=true, description="图片文件")
- * @ApiReturn ({
- "code": 1,
- "msg": "ok",
- "time": "1604727883",
- "data": "/uploads/images/202011/5fa6344bbadaa.jpg"
- })
- */
- public function upload()
- {
- $file_file='images';
- $dir = ROOT_PATH . 'public' . DS . 'uploads/'.$file_file.'/'.date('Ym').'/';
- if (!file_exists($dir)) {
- mkdir($dir, 0700,true);
- }
- $files = $_FILES;
- $imageArr = Array();
- foreach ($files as $file) {
- $imageName = $file['name'];
- //后缀名
- $ext = strtolower(substr(strrchr($imageName,'.'),1));
- //保存文件名
- $fileName = uniqid();
- $tmp = $file['tmp_name'];
- //保存 = 路径 + 文件名 + 后缀名
- $imageSavePath = $dir.$fileName.'.'.$ext;
- $info = move_uploaded_file($tmp, $imageSavePath);
- if ($info) {
- $path = '/uploads/'.$file_file.'/'.date('Ym').'/'.$fileName.'.'.$ext;
- array_push($imageArr, $path);
- }
- }
- //最终生成的字符串路径
- $imagePathStr = implode(',', $imageArr);
- //return $imagePathStr;
- $this->result('ok', $imagePathStr, 1);
- }
- }
|