I have the following function to produce the mime type from a file name:
function get_mime_type($file) {
if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file);
finfo_close($finfo);
}
else {
$mimetype = mime_content_type($file);
}
if (empty($mimetype)) $mimetype = 'application/octet-stream';
return $mimetype;
}
I call this function at this portion of my code:
$out['uploads'][] = array(
'filename' => $fldrow['field_value'],
'mimetype' => get_mime_type($fldrow['field_value']),
'id' => $fldrow['ID'],
);
$fldrow['field_value']
contains 'card.pdf'
I am expecting 'application/pdf'
I am getting 'application/octet-stream'
I also tried this more elaborate approach using Mode=1: PHP Mime type checking alternative way of doing it?
Same results in Mode=1 and blank in Mode=0.
What may I be doing wrong here?
EDIT My solution based on Dymen1's response and after looking at other posts in that direction is the following:
function get_mime_type($filename) {
$idx = explode( '.', $filename );
$count_explode = count($idx);
$idx = strtolower($idx[$count_explode-1]);
$mimet = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'docx' => 'application/msword',
'xlsx' => 'application/vnd.ms-excel',
'pptx' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
if (isset( $mimet[$idx] )) {
return $mimet[$idx];
} else {
return 'application/octet-stream';
}
}
There are a few different ways to extract the extension from a filename with PHP, which is given below: Using pathinfo() function: This function returns information about a file. If the second optional parameter is omitted, an associative array containing dirname, basename, extension, and the filename will be returned.
Expected MIME type is "text/csv", "application/vnd.
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)
I was trying to get mimeType of file using mime_content_type() and get_mime_type() but it was not working.
Here this works for me
$file = $request->file('FILE_NAME_IN_REQUEST');
$mimeType = $file->getClientmimeType();
*NOTE: function will receive $request which is coming from .blade file using the input field having type = file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With