Solved

Retrieve file extension according its MIME type

Posted on 2007-03-20
4
219 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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, hernst42 got there first. I apologize for the cross-post.
0
 
LVL 48

Expert Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now