Solved

Retrieve file extension according its MIME type

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

810 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