---
title: "How do you check if a file is an image, PDF, or audio file by inspecting its content?"  
description: "How do you check if a file is an image, PDF, or audio file by inspecting its content?"  
author: "ICSM Computer"  
published: 2025-05-16  
updated: 2025-05-16  
canonical: https://www.mindstick.com/interview/34131/how-do-you-check-if-a-file-is-an-image-pdf-or-audio-file-by-inspecting-its-content  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# How do you check if a file is an image, PDF, or audio file by inspecting its content?

To check if a file is an image, PDF, or audio file **by inspecting its content**, you should analyze the **file signature** (also known as the "magic number")—a specific byte pattern at the beginning of the file that indicates its type.

### 1. Read the File Header (Magic Number)

Here's a C# example:

```cs
public static string GetFileType(string filePath)
{
    byte[] header = new byte[8];
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        fs.Read(header, 0, header.Length);
    }

    // Check against known signatures
    if (header.Take(3).SequenceEqual(new byte[] { 0xFF, 0xD8, 0xFF }))
        return "JPEG image";

    if (header.Take(8).SequenceEqual(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }))
        return "PNG image";

    if (header.Take(4).SequenceEqual(new byte[] { 0x25, 0x50, 0x44, 0x46 }))
        return "PDF document";

    if (header.Take(4).SequenceEqual(new byte[] { 0x49, 0x44, 0x33 })) // or use offset for ID3v2
        return "MP3 audio";

    if (header.Take(4).SequenceEqual(new byte[] { 0x52, 0x49, 0x46, 0x46 }))
        return "WAV or AVI (need more info)";

    return "Unknown";
}
```

### 2. Common Signatures

| File Type | Magic Number (Hex) | Description |
| --- | --- | --- |
| JPEG | `FF D8 FF` | First 3 bytes |
| PNG | `89 50 4E 47 0D 0A 1A 0A` | First 8 bytes |
| PDF | `25 50 44 46` | `%PDF` |
| MP3 | `49 44 33` | `ID3` at start (ID3v2) |
| WAV | `52 49 46 46` | `RIFF` (check next bytes) |
| GIF | `47 49 46 38` | `GIF8` |

To distinguish between `WAV` and `AVI`, you'd need to check the next few bytes after `RIFF`.

### 3. Notes

1. This method is **more secure and reliable** than using file extensions.
2. To support more formats, extend the signature table.
3. This approach works well for validation before saving or processing files.

## Answers

### Answer by ICSM Computer

To check if a file is an image, PDF, or audio file **by inspecting its content**, you should analyze the **file signature** (also known as the "magic number")—a specific byte pattern at the beginning of the file that indicates its type.

### 1. Read the File Header (Magic Number)

Here's a C# example:

```cs
public static string GetFileType(string filePath)
{
    byte[] header = new byte[8];
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        fs.Read(header, 0, header.Length);
    }

    // Check against known signatures
    if (header.Take(3).SequenceEqual(new byte[] { 0xFF, 0xD8, 0xFF }))
        return "JPEG image";

    if (header.Take(8).SequenceEqual(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }))
        return "PNG image";

    if (header.Take(4).SequenceEqual(new byte[] { 0x25, 0x50, 0x44, 0x46 }))
        return "PDF document";

    if (header.Take(4).SequenceEqual(new byte[] { 0x49, 0x44, 0x33 })) // or use offset for ID3v2
        return "MP3 audio";

    if (header.Take(4).SequenceEqual(new byte[] { 0x52, 0x49, 0x46, 0x46 }))
        return "WAV or AVI (need more info)";

    return "Unknown";
}
```

### 2. Common Signatures

| File Type | Magic Number (Hex) | Description |
| --- | --- | --- |
| JPEG | `FF D8 FF` | First 3 bytes |
| PNG | `89 50 4E 47 0D 0A 1A 0A` | First 8 bytes |
| PDF | `25 50 44 46` | `%PDF` |
| MP3 | `49 44 33` | `ID3` at start (ID3v2) |
| WAV | `52 49 46 46` | `RIFF` (check next bytes) |
| GIF | `47 49 46 38` | `GIF8` |

To distinguish between `WAV` and `AVI`, you'd need to check the next few bytes after `RIFF`.

### 3. Notes

1. This method is **more secure and reliable** than using file extensions.
2. To support more formats, extend the signature table.
3. This approach works well for validation before saving or processing files.


---

Original Source: https://www.mindstick.com/interview/34131/how-do-you-check-if-a-file-is-an-image-pdf-or-audio-file-by-inspecting-its-content

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
