How to Determine the Extension(S) Associated with a Mime Type in PHP

How do I determine the extension(s) associated with a MIME type in PHP?

Not built-in, but it's not terribly hard to roll your own:

function system_extension_mime_types() {
# Returns the system MIME type mapping of extensions to MIME types, as defined in /etc/mime.types.
$out = array();
$file = fopen('/etc/mime.types', 'r');
while(($line = fgets($file)) !== false) {
$line = trim(preg_replace('/#.*/', '', $line));
if(!$line)
continue;
$parts = preg_split('/\s+/', $line);
if(count($parts) == 1)
continue;
$type = array_shift($parts);
foreach($parts as $part)
$out[$part] = $type;
}
fclose($file);
return $out;
}

function system_extension_mime_type($file) {
# Returns the system MIME type (as defined in /etc/mime.types) for the filename specified.
#
# $file - the filename to examine
static $types;
if(!isset($types))
$types = system_extension_mime_types();
$ext = pathinfo($file, PATHINFO_EXTENSION);
if(!$ext)
$ext = $file;
$ext = strtolower($ext);
return isset($types[$ext]) ? $types[$ext] : null;
}

function system_mime_type_extensions() {
# Returns the system MIME type mapping of MIME types to extensions, as defined in /etc/mime.types (considering the first
# extension listed to be canonical).
$out = array();
$file = fopen('/etc/mime.types', 'r');
while(($line = fgets($file)) !== false) {
$line = trim(preg_replace('/#.*/', '', $line));
if(!$line)
continue;
$parts = preg_split('/\s+/', $line);
if(count($parts) == 1)
continue;
$type = array_shift($parts);
if(!isset($out[$type]))
$out[$type] = array_shift($parts);
}
fclose($file);
return $out;
}

function system_mime_type_extension($type) {
# Returns the canonical file extension for the MIME type specified, as defined in /etc/mime.types (considering the first
# extension listed to be canonical).
#
# $type - the MIME type
static $exts;
if(!isset($exts))
$exts = system_mime_type_extensions();
return isset($exts[$type]) ? $exts[$type] : null;
}

PHP: File extension to MIME type?

try this to get info about which extension is which mime type, but of course please be aware that this won't be very accurate (e.g. possible gif file on png extension..)

Convert MIME type to file Extension PHP

You could use something like:

function getExtension ($mime_type){

$extensions = array('image/jpeg' => 'jpeg',
'text/xml' => 'xml'
);

// Add as many other Mime Types / File Extensions as you like

return $extensions[$mime_type];

}

Note: not every MIME type has a fixed file extension. Also, MIME types like application/octet-stream can refer to multiple file extensions.

PHP: How to properly check MIME type of a file?

To get MIME type, developers generally depend on $_FILES['input_name']['type']. But this is absolutely vulnerable. Because a malicious user can set one of image/jpg, image/png, image/gif etc. MIME types to a file that is not actually an image. In that case, the malicious user may get your script pass to upload other files instead of an image and execute your script for their purposes which is dangerous.

So I recommend that you not depend on the following snippet to get MIME of a file

$_FILES['input_name']['type'];

Rather I would recommend that you use this mime_content_type() function to get MIME type but with the help of other PHP's built-in functions. And that is is_uploaded_file() function. What it does is:

This is useful to help ensure that a malicious user hasn't tried to
trick the script into working on files upon which it should not be
working--for instance, /etc/passwd.

This sort of check is especially important if there is any chance that
anything done with uploaded files could reveal their contents to the
user, or even to other users on the same system.

So to make this function work properly it needs a specific argument. Check out the code below:

if (is_uploaded_file($_FILES['input_name']['tmp_name'])) {
// Do other stuff.
}

This function returns true on success, false otherwise. So if it returns true then you're ok with the file. Thanks to this function. Now mime_content_type() function comes into play. How? Look at the code below:

if (is_uploaded_file($_FILES['input_name']['tmp_name'])) {
// Notice how to grab MIME type.
$mime_type = mime_content_type($_FILES['input_name']['tmp_name']);

// If you want to allow certain files
$allowed_file_types = ['image/png', 'image/jpeg', 'application/pdf'];
if (! in_array($mime_type, $allowed_file_types)) {
// File type is NOT allowed.
}

// Set up destination of the file
$destination = '/path/to/move/your/file/';

// Now you move/upload your file
if (move_uploaded_file ($_FILES['input_name']['tmp_name'] , $destination)) {
// File moved to the destination
}
}

BTW, for novice, do not try remote URL with this function to get MIME type. The code below will not work:

mime_content_type('http://www.example.com/uploads/example.png');

But the one below would work:

mime_content_type('/source/to/your/file/etc.png');

Hope you would enjoy uploading files from now on.

Accessing Apache's Mime-Type to Extension Mapping

You're probably running php as a CGI, but if you are using mod_php and the file is accessible via a url, you can take a look at:

http://us2.php.net/manual/en/function.apache-lookup-uri.php

Or if you aren't limited by openbasedir restrictions, you could loop over file('/usr/local/apache2/conf/mime.types')

Mime Type vs Extension Check, which method is better to check file type?

Use the first one, because not every MIME type has a fixed file extension. Also, MIME types like application/octet-stream can refer to multiple file extensions. and also you can use How to extract a file extension in PHP?

Not every MIME type has a fixed file extension...

Does every file have a MIME type associated with it?

After a few days of research and advice, the answer to the question is kind of irrelevant due to the fact that checking for MIME types as a security feature is not feasible in the first place. There are too many issues with MIME types on different operating systems, different applications saving files differently, some files not having a MIME at all, and lastly, the fact that the extension and MIME could be altered by a malicious user or program. Closing.

Getting mime type from file name in php

If you check the documentation, you can see that you are not doing anything wrong.
But if you do a bit more research:

https://stackoverflow.com/a/3664655/3784145

you can see that the mime type you get is correct, but the extension doesn't need to match with the mime type as explained here:

http://nl3.php.net/manual/en/function.mime-content-type.php#85879

I would therefore use the files suffix to determine the files mime type.
(as seen in the first example)

check compatibility between file extensions with mimetype in php

You should be doing it like this

// MIME types must be array even if there is only 1 of them
$fileAllows = array(
"rar"=>array("application/x-rar"),
"xls"=>array(
"application/vnd.ms-office",
"application/x-msexcel",
"application/x-excel",
"application/excel",
"application/vnd.ms-excel",
)
);

$mimeInfo = mimeInfo($pathfile);
$file = pathinfo($pathfile);
$ext = strtolower($file['extension']); // convert to lowercase

if(is_array($fileAllows[$ext])) return in_array($mimeInfo, $fileAllows[$ext]);
else return false;


Related Topics



Leave a reply



Submit