{"record":{"id":"010f7c2657193c57","repo":"gofiber/fiber","slug":"file-file-header-is-nil","errorCode":null,"errorMessage":"file: file header is nil","messagePattern":"file: file header is nil","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"error.go","lineNumber":102,"sourceCode":"\n\t// SyntaxError is a description of a JSON syntax error.\n\tSyntaxError = json.SyntaxError\n\n\t// UnmarshalTypeError describes a JSON value that was\n\t// not appropriate for a value of a specific Go type.\n\tUnmarshalTypeError = json.UnmarshalTypeError\n\n\t// UnsupportedTypeError is returned by Marshal when attempting\n\t// to encode an unsupported value type.\n\tUnsupportedTypeError = json.UnsupportedTypeError\n\n\t// UnsupportedValueError exposes json.UnsupportedValueError to describe unsupported values encountered during encoding.\n\tUnsupportedValueError = json.UnsupportedValueError\n)\n\n// File errors\nvar (\n\tErrFileHeaderNil = errors.New(\"file: file header is nil\")\n\tErrFileOpen      = errors.New(\"file: failed to open file\")\n\tErrFileRead      = errors.New(\"file: failed to read file\")\n\tErrFileStore     = errors.New(\"file: failed to store file\")\n)\n","sourceCodeStart":84,"sourceCodeEnd":107,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/error.go#L84-L107","documentation":"ErrFileHeaderNil is returned by DefaultCtx.SaveFile (ctx.go:559) and DefaultCtx.SaveFileToStorage (ctx.go:567) when the caller passes a nil *multipart.FileHeader. The methods need a non-nil header to open the underlying multipart part; nil short-circuits before any filesystem operation.","triggerScenarios":"Passing a nil pointer to c.SaveFile(fh, path) or c.SaveFileToStorage(fh, path, storage). Common when c.FormFile(field) returned an error that was ignored and the header was never assigned, or when iterating a missing multipart form.","commonSituations":"Ignoring the error from c.FormFile and using the zero-value *multipart.FileHeader; uploading without enctype=multipart/form-data so the form is empty; iterating c.MultipartForm().File and dereferencing absent keys.","solutions":["Check the error from c.FormFile before calling SaveFile: fh, err := c.FormFile(\"file\"); if err != nil { return err }.","Before iterating multipart files, confirm c.MultipartForm() is non-nil and the target key exists.","Add an explicit nil check on the header so the failure points at your code rather than the library."],"exampleFix":"// before\nfh, _ := c.FormFile(\"file\")\nreturn c.SaveFile(fh, \"./uploads/\"+fh.Filename)\n// after\nfh, err := c.FormFile(\"file\")\nif err != nil {\n    return err\n}\nreturn c.SaveFile(fh, \"./uploads/\"+fh.Filename)","handlingStrategy":"validation","validationCode":"// Always check FormFile's error before touching the header.\nfh, err := c.FormFile(\"file\")\nif err != nil {\n    return c.Status(fiber.StatusBadRequest).SendString(\"file required\")\n}\nif fh == nil {\n    return c.Status(fiber.StatusBadRequest).SendString(\"file header missing\")\n}\nreturn c.SaveFile(fh, \"./uploads/\"+fh.Filename)","typeGuard":null,"tryCatchPattern":"if err := c.SaveFile(fh, path); err != nil {\n    if errors.Is(err, fiber.ErrFileHeaderNil) {\n        return c.Status(fiber.StatusBadRequest).SendString(\"no file uploaded\")\n    }\n    return err\n}","preventionTips":["Never ignore the error from c.FormFile.","Confirm the form is multipart (enctype=multipart/form-data) before reading files.","Add an explicit nil check on the FileHeader so failures point at your handler."],"tags":["file","upload","multipart","validation"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}