Skip to content

Instantly share code, notes, and snippets.

@neonxp
Created March 5, 2012 14:01
Show Gist options
  • Save neonxp/1978424 to your computer and use it in GitHub Desktop.
Save neonxp/1978424 to your computer and use it in GitHub Desktop.
Rocksolid function for get extension of file by it mime type
<?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