isValid()) { $ext = $file->getUploadExtension(); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'JPG', 'JPEG', 'PBG'])) { $type = 'images'; } else { $type = 'other'; } $fileSize = $file->getSize(); $uploadDir = '/files/' . $type . '/' . date('Ym') . '/'; $filename = date('YmdHis') . rand(999, 99999); $uploadPath = $uploadDir . $filename . '.' . $ext; $rootUploadDir = public_path() . $uploadDir; if (!is_dir($rootUploadDir)) { mkdir($rootUploadDir, 0777, true); } $filePath = public_path() . $uploadPath; $file->move($filePath); //上传到七牛云 $res = QiniuUtils::upload($filePath); if ($res['code'] == ResponseCode::FAIL) { throw new Exception($res['msg']); } //删除本地图片 unlink($filePath); return [ 'code' => ResponseCode::SUCCESS, 'msg' => '上传成功', 'data' => $res['data'] ]; } else { throw new Exception('文件无效'); } } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } public function uploadFile($file) { ini_set('memory_limit', '256M'); try { if ($file && $file->isValid()) { $ext = $file->getUploadExtension(); if (in_array($ext, ['pdf', 'doc', 'docx', 'xlsx', 'xls', 'csv', 'pptx', 'ppt', 'zip', 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'JPG', 'JPEG', 'PBG'])) { $type = 'files'; } else { $type = 'other'; } $fileSize = $file->getSize(); $origin_name = $file->getUploadName(); $uploadDir = '/files/' . $type . '/' . date('Ym') . '/'; $filename = date('YmdHis') . rand(999, 99999); $uploadPath = $uploadDir . $filename . '.' . $ext; $rootUploadDir = public_path() . $uploadDir; if (!is_dir($rootUploadDir)) { mkdir($rootUploadDir, 0777, true); } $filePath = public_path() . $uploadPath; $file->move($filePath); //上传到七牛云 $res = QiniuUtils::upload($filePath); if ($res['code'] == ResponseCode::FAIL) { throw new Exception($res['msg']); } //删除本地图片 unlink($filePath); return [ 'code' => ResponseCode::SUCCESS, 'msg' => '上传成功', 'data' => [ 'url' => $res['data'], 'origin_name' => $origin_name ] ]; } else { throw new Exception('文件无效'); } } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } }