Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

Get Content Type

Dawn Rossi, 10-15-2007
Nifty function to get the MIME Content Type for a given filename. Supports all popular formats:


function ContentTypeGet($filename)
{
   
// Initialize     
   
$result = "";

   
// Extract file extension
   
$filename_extension = strrchr($filename,".");
    if (!empty(
$filename_extension))
    {
       
$filename_extension = strtolower(substr($filename_extension,1));

       
// Load content types
       
$file = @fopen("contenttypes.csv","r");
        if (!
$file) break;
        while (!
feof($file))
        {
       
$line = fgets($file, 4096);
       
$line = str_replace("r","",$line);
       
$line = str_replace("n","",$line);

        @list(
$extensions, $contenttype) = split(",",$line);

       
$arr_extensions = explode(" ",$extensions);

        foreach (
$arr_extensions as $ext)
        {
           
$arr_contenttype[$ext] = $contenttype;
        }
        }
        @
fclose($file);

       
$result = $arr_contenttype[$filename_extension];
    }

   
// If n result, default to octstream-binary
   
if (empty($result)) $result = "binary/octet-stream";

   
// Return result
   
return $result;

}

The data file can be downloaded here
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions