FormatterHQ

Developer Tools

What File Type Is This? File Signatures (Magic Numbers) Explained

A file signature tells you what a file really is, whatever its name says. Common magic numbers in hex, why DOCX is a ZIP, how to read the first bytes and how to check uploads safely.

Published 7 min read

You download a file called report.pdf, your PDF reader refuses to open it, and you start to wonder what it really is. The answer is usually in its first few bytes. Most binary formats begin with a fixed byte sequence called a file signature, or magic number, and reading it tells you the real format no matter what the file is named. This guide lists common signatures in hex and ASCII, explains why Office documents look like ZIP archives, shows how to read the first bytes yourself, and covers what upload code should check.

An extension is just part of the name

The .pdf at the end of a file name is a hint for the operating system about which app to open it with. Nothing stops anyone from renaming setup.exe to setup.pdf; the bytes inside stay exactly the same. Extensions also get lost or mangled in practice: email gateways rename attachments, downloads arrive without an extension, and some apps save files with the wrong one.

Windows File Explorer hides extensions for known file types by default, which makes things worse. A file named invoice.pdf.exe is shown as invoice.pdf, and if the program inside sets a PDF-looking icon, nothing on screen gives it away.

What a file signature is

A file signature is a short sequence of bytes, usually at the very start of the file (offset 0), that a format's specification requires every file to begin with. Programs use it to confirm they were handed the right kind of data before parsing the rest. The idea comes from Unix, where the file command has long identified files from a database of these byte patterns.

Some signatures are readable text, like %PDF-. Others mix in bytes that never appear in normal text, which makes accidental matches unlikely. The PNG signature is a nice example: its 0D 0A (CR LF), 1A and first byte with the high bit set are there so that a transfer that rewrites line endings or strips the eighth bit visibly breaks the signature.

Common file signatures in hex and ASCII

Dots in the ASCII column stand for bytes that are not printable characters.

FormatFirst bytes (hex)As ASCII
PDF25 50 44 46 2D%PDF- followed by the version, such as 1.7
PNG89 50 4E 47 0D 0A 1A 0A.PNG....
JPEGFF D8 FFnot printable
GIF47 49 46 38 37 61 or 47 49 46 38 39 61GIF87a or GIF89a
ZIP (and DOCX, XLSX, JAR, APK)50 4B 03 04PK..
Windows EXE or DLL4D 5AMZ
Linux ELF executable7F 45 4C 46.ELF
gzip (.gz, .tgz)1F 8Bnot printable
7-Zip37 7A BC AF 27 1C7z....
MP3 with an ID3v2 tag49 44 33ID3
WAV52 49 46 46 + 4 size bytes + 57 41 56 45RIFF....WAVE
WebP52 49 46 46 + 4 size bytes + 57 45 42 50RIFF....WEBP

The PK in ZIP stands for Phil Katz, the creator of PKZIP, and MZ for Mark Zbikowski, one of the MS-DOS developers. WAV and WebP show why a signature is sometimes more than one fixed block: both use the RIFF container, whose bytes 4 to 7 hold the size of the rest of the file, and only bytes 8 to 11 name the actual format. A checker that stops at RIFF cannot tell a song from a picture.

Why DOCX, XLSX, JAR and APK all look like ZIP files

Many modern formats are ZIP archives with an agreed set of files inside. Their file signature is the plain ZIP one, so the first bytes alone only say "this is a ZIP". To go further you have to look at the entries in the archive:

  • DOCX, XLSX, PPTX: a [Content_Types].xml entry plus word/document.xml, xl/workbook.xml or ppt/presentation.xml respectively.
  • JAR: Java classes, usually with a META-INF/MANIFEST.MF entry.
  • APK: an Android package with AndroidManifest.xml and classes.dex.
  • ODT, ODS, ODP and EPUB: the first entry is an uncompressed file named mimetype whose content, such as application/vnd.oasis.opendocument.text or application/epub+zip, is readable near the start of the file.

This is also why renaming report.docx to report.zip lets you open it and look at the XML inside.

Text files have no signature

Plain text, CSV, JSON, HTML, XML and source code have no magic number. Their first bytes are simply the first characters of the content, so a tool has to look at the content itself: does it start with { or [ and parse as JSON, does it start with <?xml or <svg, does it contain only valid UTF-8?

The one marker some text files carry is a byte order mark (BOM), which reveals the encoding:

EncodingBOM (hex)
UTF-8EF BB BF (optional, and often absent)
UTF-16 little-endianFF FE
UTF-16 big-endianFE FF

A UTF-8 BOM is a common reason for a CSV file's first column header or a JSON file failing to parse in tools that do not expect it.

How to read the first bytes yourself

On macOS and Linux, xxd prints a hex dump. Limit it to the first 16 or 32 bytes:

$ xxd -l 32 report.pdf
00000000: 2550 4446 2d31 2e37 0a25 e2e3 cfd3 0a31  %PDF-1.7.%.....1
00000010: 2030 206f 626a 0a                         0 obj.

The 25 50 44 46 2D at the start confirms a real PDF. The file command does the matching for you and prints a description such as report.pdf: PDF document, version 1.7. An EXE renamed to .pdf looks very different:

$ xxd -l 16 report.pdf
00000000: 4d5a 9000 0300 0000 0400 0000 ffff 0000  MZ..............

In Windows PowerShell, Format-Hex prints the same kind of dump, 16 bytes per row:

Format-Hex -Path .\report.pdf | Select-Object -First 2

If you would rather not use a terminal, the file type checker reads the first bytes in your browser, matches them against more than 150 signatures, looks inside ZIP files to tell DOCX, XLSX, PPTX, JAR and APK apart, and recognizes text formats such as JSON, SVG and HTML, including their encoding and BOM. It compares the result with the extension, shows a hex dump of the first 64 bytes, and checks many files at once without uploading them.

Disguised executables and what a format check cannot do

Two tricks come up again and again. The first is the double extension, invoice.pdf.exe, which relies on hidden extensions. The second is an executable renamed to .pdf or .jpg. Double-clicking it opens a PDF reader that fails, but an executable with a document name is a strong sign that someone tried to get it past a filter that only checks names. The file type checker flags executables disguised as documents or images in red.

A matching signature only means the file is the format it claims to be. It does not mean the file is safe. A genuine PDF can contain JavaScript or target a reader bug, and a genuine Word file can carry macros. A format check is not a virus scan, so keep your antivirus and treat unexpected attachments with care even when the type checks out.

For developers: never trust the extension or the browser MIME type

An upload form receives a file name and a Content-Type header from the client. Browsers usually derive that type from the extension, and anyone using a script can send whatever values they like. Neither proves anything about the bytes.

  • Check the signature on the server. A check in the browser is good for quick feedback but trivial to bypass. Most languages wrap libmagic, the library behind file; in PHP that is finfo.
  • Use an allow-list of the types you expect, not a list of types you want to block.
  • Re-encode images where possible, so whatever was appended to a valid image does not survive.
  • Store uploads outside the web root with generated names, and serve them with an explicit Content-Type plus X-Content-Type-Options: nosniff.
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = $finfo->file($_FILES['upload']['tmp_name']);

$allowed = ['application/pdf', 'image/png', 'image/jpeg'];
if (!in_array($mime, $allowed, true)) {
    http_response_code(415);
    exit('Unsupported file type');
}

Laravel's mimes and mimetypes validation rules also read the file contents rather than trusting the client. When you need the standard MIME type for an extension, for an allow-list or a response header, the MIME type lookup has about 150 common ones.

Signatures in Base64 and checksums

Signatures survive encoding in a recognizable form. A Base64 string or data URI that starts with JVBERi0 is a PDF, iVBORw0KGgo is a PNG and /9j/ is a JPEG, which helps when you are debugging an API payload. You can see this by converting a file with file to Base64.

Knowing the type is one question; knowing the file is the exact one the publisher released is another. For downloads that come with a published SHA-256 hash, compare it with the output of the checksum calculator before you open the file.

  • file signature
  • magic numbers
  • file types
  • upload security
  • hex dump

Frequently asked questions

Can a file have no extension and still open correctly?
Yes. Many programs read the first bytes and recognize the format regardless of the name, so opening the file from inside the right app usually works. Adding the matching extension back makes it open in the right app with a double-click.
Can a file signature be faked?
Yes. Anyone can write the right first bytes at the start of a file, and some files are crafted to be valid in two formats at once. That is why upload handling should also limit size, re-encode images where possible and never execute uploaded files.
Why does my DOCX file show up as a ZIP archive?
Word documents in the DOCX format are ZIP archives of XML files, so their first bytes are the ZIP signature. A tool that only reads the first bytes reports ZIP; one that looks at the entries inside can tell it is a Word document.
Is a file safe if its type matches its extension?
Not necessarily. A real PDF or Office file can still contain harmful scripts or macros. A type check catches disguised files, while an up-to-date antivirus scanner looks for malicious content.
What is the difference between a file signature and a checksum?
A signature is a fixed pattern shared by every file of a format, so it tells you the type. A checksum such as SHA-256 is calculated from all of the bytes of one specific file, so it tells you whether that exact file has changed.

Tools used in this guide