Solved

Retrieve file extension according its MIME type

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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