Solved

Retrieve file extension according its MIME type

Posted on 2007-03-20
4
230 Views
Last Modified: 2010-08-05
I have files stored as BLOB records in database, with no further information. I want users to be able to download them, so first I save that BLOB to file and then use "system('file -i -b filename')" command to obtain its MIME type. Now I need to retrieve file extention from that MIME type (so that when user downloads the file, his operating system can open it correctly). Any idea how to retrieve it ?
0
Comment
Question by:protein007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 18757130
There is no php-function or program which will tell you which ending to use. apache ships a mime.types where the application-types are listed with their possible endings. So read with php the mime.types-file and search of the application-type you got from the file-command and use the first extension in the file. The file looks like:

image/ief ief
image/jpeg jpeg jpg jpe
image/jpeg2000 jp2
image/png png
image/rle rle
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18757160
If the filename was not stored, I do not believe that there will be an automated way to do this. You will need to associate the extensions with the MIME types as needed. For example:

switch ($mimeType)
{
   case 'image/jpeg':
      $extension = 'jpg';
      break;
   case 'image/bmp':
      $extension = 'bmp';
      break;
   case 'application/xml':
      $extension = 'xml';
      break;
}

You will just need to create a case for each potential MIME type stored in your database. You can use http://www.ltsw.se/knbase/internet/mime.htp to retrieve a list of MIME types.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18757168
Sorry, hernst42 got there first. I apologize for the cross-post.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18757206
It's no cross post, nor its the same solution so let the questioneer decide which solutions fits better for him.
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question