file(); // 验证上传文件格式和大小 $this->validate($files, Validate::rule(['file' => 'fileSize:2048000|fileExt:jpeg,jpg,png,webp'])); $file = request()->file('file'); try { // 保存上传文件到public目录下的$path目录中 $path = Filesystem::disk('public')->putFile($path, $file); } catch (\Exception $e) { throw $this->warpException($e, '上传图片失败:\n'); } // 获取图片的url地址 $path = Request::domain() . getVirRootDir() . '/storage/' . $path; return [ 'path' => $path ]; } /** * 上传附件 * 本地运行如果无法写入图片可能是没有给public目录写权限 * * @param string $path 上传路径,默认为'default' * @return array 上传后的图片路径 */ public function uploadAttachment($path = 'default') { // 获取表单上传文件 $files = request()->file(); // 验证上传文件格式和大小 $this->validate($files, Validate::rule(['file' => 'fileExt:pdf,doc,docx'])); $file = request()->file('file'); try { // 保存上传文件到public目录下的$path目录中 $path = Filesystem::disk('public')->putFile($path, $file); } catch (\Exception $e) { throw $this->warpException($e, '上传图片失败:\n'); } // 获取图片的url地址 $path = Request::domain() . getVirRootDir() . '/storage/' . $path; return [ 'path' => $path ]; } /** * 获取当前网站的域名地址 * * @return string 域名地址 */ protected static function get_domain() { $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://'; return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''); } }