Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:45:59+00:00 2026-06-04T09:45:59+00:00

Detecting the MIME type of a file with PHP is trivial – just use

  • 0

Detecting the MIME type of a file with PHP is trivial – just use PEAR’s MIME_Type package, PHP’s fileinfo or call file -i on a Unix machine.
This works really well for binary files and all others that have some kind of “magic bytes” through which they can be detected easily.

What I’m failing at is detecting the correct MIME type of plain text files:

  • CSS
  • Diff
  • INI (configuration)
  • Javascript
  • rST
  • SQL

All of them are identified as “text/plain”, which is correct, but too unspecific for me. I need the real type, even if it costs some time to analyze the file content.

So my question: Which solutions exist to detect the MIME type of such plain text files? Any Libraries? Code snippets?


Note that I neither have a filename nor a file extension, but I have the file content.


If I used ruby, I could integrate github’s linguist. Ohloh’s ohcount is written in C, but has a command line tool to detect the type: ohcount -d $file

What I’ve tried

ohcount

Detects xml and php files correctly, all other not.

Apache tika

Detects xml and html, all other tests files were only seen as text/plain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:46:01+00:00Added an answer on June 4, 2026 at 9:46 am

    Since I didn’t find a proper library, I wrote my own magic file that detects all of my test files properly.

    My application first tries my custom magic file for detection and falls back to the normal/system magic file if no type is detected.

    The code it on github, see https://github.com/cweiske/MIME_Type_PlainDetect .
    The magic file is at data/programming.magic and can be used with file -f programming.magic /path/to/source

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am interested in detecting the MIME-type for a file in the documents directory
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Is there a quick way of reliably detecting the compression type of a file
For detecting Internet Explorer I use his line. <?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
When detecting the encoding of some text from Word (saved as a CSV file)
I'm detecting @replies in a Twitter stream with the following PHP code using regexes.
I want to detecting when a user clicks OUT of an input type Text
I've had this question for a while: how exactly is the mime type of
Is there an efficient way of detecting if a jpeg file is corrupted? Background
In this question: Detecting duplicate lines on file using c i can detect duplicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.