Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Detecting file types using byte-level inspection (also called
magic number checking) involves reading the beginning bytes of a file (the "magic number") to determine its format. This is more reliable than checking file extensions.
Here's how you can implement file type detection (e.g., IsPdf,
IsImage, etc.) by inspecting the byte content:
Step-by-Step Overview
Read the header bytes of the file (usually the first 4–8 bytes).
Match the byte signature against known file type "magic numbers."
Optionally, allow some fuzziness for formats like image types with common variations.
Common File Type Magic Numbers
File Type
Magic Number (Hex)
ASCII
Description
PDF
25 50 44 46
%PDF
PDF files start with %PDF
PNG
89 50 4E 47 0D 0A 1A 0A
—
PNG image
JPEG
FF D8 FF
—
JPEG image
GIF
47 49 46 38
GIF8
GIF image (GIF87a/GIF89a)
ZIP
50 4B 03 04
PK..
ZIP archives (also DOCX, XLSX, etc.)
Example: Detect PDF and Image Types in C#
public static class FileTypeDetector
{
public static bool IsPdf(byte[] bytes)
{
// Check if the first 4 bytes are "%PDF"
return bytes.Length >= 4 &&
bytes[0] == 0x25 && // %
bytes[1] == 0x50 && // P
bytes[2] == 0x44 && // D
bytes[3] == 0x46; // F
}
public static bool IsPng(byte[] bytes)
{
byte[] pngSignature = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
return bytes.Take(pngSignature.Length).SequenceEqual(pngSignature);
}
public static bool IsJpeg(byte[] bytes)
{
return bytes.Length >= 3 &&
bytes[0] == 0xFF &&
bytes[1] == 0xD8 &&
bytes[2] == 0xFF;
}
public static bool IsGif(byte[] bytes)
{
return bytes.Length >= 4 &&
bytes[0] == 0x47 && // G
bytes[1] == 0x49 && // I
bytes[2] == 0x46 && // F
bytes[3] == 0x38; // 8
}
public static bool IsZip(byte[] bytes)
{
return bytes.Length >= 4 &&
bytes[0] == 0x50 && // P
bytes[1] == 0x4B && // K
bytes[2] == 0x03 &&
bytes[3] == 0x04;
}
public static bool IsImage(byte[] bytes)
{
return IsPng(bytes) || IsJpeg(bytes) || IsGif(bytes);
}
}
Usage Example
byte[] fileBytes = File.ReadAllBytes("example.pdf");
if (FileTypeDetector.IsPdf(fileBytes))
{
Console.WriteLine("This is a PDF file.");
}
else if (FileTypeDetector.IsImage(fileBytes))
{
Console.WriteLine("This is an image file.");
}
Benefits
Works regardless of file extension.
Safer when dealing with user-uploaded files.
Can easily be extended to support more file types.
Liked By
Write Answer
How can you detect file type using byte-level inspection (e.g., IsPdf, IsImage)?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Join MindStick Community
You have need login or register for voting of answers or question.
Anubhav Kumar
25-May-2025Detecting file types using byte-level inspection (also called magic number checking) involves reading the beginning bytes of a file (the "magic number") to determine its format. This is more reliable than checking file extensions.
Here's how you can implement file type detection (e.g.,
IsPdf,IsImage, etc.) by inspecting the byte content:Step-by-Step Overview
Common File Type Magic Numbers
25 50 44 46%PDF%PDF89 50 4E 47 0D 0A 1A 0AFF D8 FF47 49 46 38GIF850 4B 03 04PK..Example: Detect PDF and Image Types in C#
Usage Example
Benefits