Created
March 5, 2012 14:01
-
-
Save neonxp/1978424 to your computer and use it in GitHub Desktop.
Rocksolid function for get extension of file by it mime type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getExtByMimeType($mime) { | |
$known_files = array( | |
'/etc/mime.types', | |
'/etc/httpd/mime.types', | |
'C:\Program Files\Apache Group\Apache\conf\mime.types' | |
); /* If mime type file of your system not listed here, please add it path here */ | |
$extensionFile = null; | |
for ($i = 0; $i<count($known_files); $i++) { | |
if (file_exists($known_files[$i])) { | |
$extensionFile = file($known_files[$i]); | |
break; | |
} | |
} | |
if ($extensionFile === null) { | |
return false; | |
} | |
foreach($extensionFile as $line) { | |
if ($line[0] != '#') { | |
$parts = explode("\t",$line); | |
if (strtolower($parts[0])==strtolower($mime)) { | |
$extensions = strtolower($parts[count($parts)-1]); | |
list($extension, ) = explode(' ', $extensions, 2); | |
return $extension; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment