Solved

Retrieve file extension according its MIME type

Posted on 2007-03-20
4
224 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

808 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