payloadcms/payload · error · ValidationError

Could not read uploaded file for type detection.

Error message

Could not read uploaded file for type detection.

What it means

Thrown when the file-type library's fileTypeFromFile(tempFilePath) or fileTypeFromBuffer(file.data) throws synchronously during the secondary mimetype-detection step (it is wrapped in try/catch). Note this is distinct from the library returning undefined/null (undetectable), which is handled gracefully — this fires only when the call actively rejects.

Source

Thrown at packages/payload/src/uploads/checkFileRestrictions.ts:143

      _fileBuffer = await fs.readFile(tempFilePath)
      return _fileBuffer
    } catch {
      throw new ValidationError({
        errors: [{ message: 'Could not read uploaded file for validation.', path: 'file' }],
      })
    }
  }

  // Secondary mimetype check to assess file type from buffer
  if (configMimeTypes.length > 0) {
    let detected
    try {
      detected =
        isTempFile && tempFilePath
          ? await fileTypeFromFile(tempFilePath)
          : await fileTypeFromBuffer(file.data)
    } catch {
      throw new ValidationError({
        errors: [{ message: 'Could not read uploaded file for type detection.', path: 'file' }],
      })
    }
    const typeFromExtension = file.name.split('.').pop() || ''

    // Handle SVG files that are detected as XML due to <?xml declarations
    if (
      detected?.mime === 'application/xml' &&
      configMimeTypes.some(
        (type) => type.includes('image/') && (type.includes('svg') || type === 'image/*'),
      )
    ) {
      const isSvg = detectSvgFromXml(await getFileBuffer())
      if (isSvg) {
        detected = { ext: 'svg', mime: 'image/svg+xml' }
      }
    }

View on GitHub (pinned to 00c58b35c0)

Solutions

  1. Confirm file.data is a populated Buffer for in-memory uploads, or tempFilePath exists for on-disk uploads, before invoking the upload pipeline.
  2. Ensure no middleware mutates or nulls file.data between multipart parse and checkFileRestrictions.
  3. Stabilize the temp directory so it survives the request (see error 241).
  4. If the error is intermittent, log the underlying cause by temporarily replacing the catch with a rethrow to inspect the file-type error.

Example fix

// before
const detected = await fileTypeFromBuffer(file.data)
// after — coerce to a valid Buffer and guard
if (!Buffer.isBuffer(file.data) || file.data.length === 0) {
  throw new Error('Empty file buffer; re-upload the file.')
}
const detected = await fileTypeFromBuffer(file.data)
Defensive patterns

Strategy: validation

Validate before calling

import { Buffer } from 'buffer'
function bufferOk(b: unknown): b is Buffer {
  return Buffer.isBuffer(b) && (b as Buffer).length > 0
}
// before validation:
if (!bufferOk(file.data) && !file.tempFilePath) throw new Error('No file content to detect')

Type guard

const isNonEmptyBuffer = (b: unknown): b is Buffer =>
  Buffer.isBuffer(b) && b.length > 0

Try / catch

try {
  await payload.create({ collection: 'media', file })
} catch (e) {
  if (e instanceof ValidationError && /Could not read uploaded file for type detection/.test(e.message)) {
    notifyOps('file-type detection failed; check temp file lifecycle')
    askUserToRetry()
  } else throw e
}

Prevention

When it happens

Trigger: fileTypeFromFile rejects because the temp path is unreadable, or fileTypeFromBuffer rejects because file.data is not a valid Buffer/Uint8Array (e.g. undefined, or a truncated stream). Reached only when upload.mimeTypes is configured and checkFileContents is true.

Common situations: File buffer was mutated/cleared by middleware before validation; temp file deleted between the existence check and the file-type read; a streaming parser that didn't fully buffer the data; version mismatch in file-type expecting a Buffer but receiving a TypedArray view.

Related errors


AI-assisted analysis of payloadcms/payload@00c58b35c0 (2026-08-12). Data as JSON: /api/errors/ac9b568502366c52. Report an issue: GitHub.