Skip to content

Instantly share code, notes, and snippets.

@flangofas
Last active May 9, 2016 09:11
Show Gist options
  • Save flangofas/a8d161c57f05400cb46d5f9f0f115c99 to your computer and use it in GitHub Desktop.
Save flangofas/a8d161c57f05400cb46d5f9f0f115c99 to your computer and use it in GitHub Desktop.
MIME types trait
<?php
namespace App\Traits;
trait MimeTypeTrait
{
/**
* MIME Types
*
* @link http://stackoverflow.com/questions/6654351/check-file-uploaded-is-in-csv-format#answer-13032788
* @link http://www.rfc-editor.org/rfc/rfc3778.txt
* @link http://filext.com/faq/office_mime_types.php
* @var array
*/
public static $mimeTypes = [
'csv' => [
'text/csv',
'text/csv',
'text/plain',
'text/comma-separated-values',
'text/anytext',
'application/csv',
'application/excel',
'application/vnd.ms-excel',
'application/vnd.msexcel',
'application/octet-stream',
'application/txt',
],
'pdf' => [
'application/pdf',
],
'word' => [
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/vnd.ms-word.document.macroEnabled.12',
'application/vnd.ms-word.template.macroEnabled.12',
],
'excel' => [
'application/vnd.ms-excel',
'application/vnd.msexcel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'application/vnd.ms-excel.sheet.macroEnabled.12',
'application/vnd.ms-excel.template.macroEnabled.12',
'application/vnd.ms-excel.addin.macroEnabled.12',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
]
];
/**
* Returns all defined MIME types.
*
* @param string|array $types MIME type
* @return array
*/
public static function getMimeTypes($types = [])
{
$result = [];
if (empty($types)) {
$types = ['csv', 'pdf', 'word', 'excel'];
}
if (is_string($types)) {
$types = (array)$types;
}
foreach ($types as $type) {
if (isset(static::$mimeTypes[$type])) {
$result = array_merge($result, static::$mimeTypes[$type]);
}
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment