Kotchasan Framework Documentation

Kotchasan Framework Documentation

Mime

EN 05 Feb 2026 07:53

Mime

\Kotchasan\Mime for handling file MIME types based on extensions

Usage

use Kotchasan\Mime;

// Get MIME type from single extension
$mime = Mime::get('pdf');  // 'application/pdf'

// Get MIME types from multiple extensions
$mimes = Mime::get(['jpg', 'png', 'gif']);
// ['jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif']

// Check MIME type
$isValid = Mime::check(['jpg', 'png'], 'image/jpeg');  // true

// Generate accept string for file input
$accept = Mime::getAccept(['jpg', 'png', 'gif']);
// 'image/jpeg,image/png,image/gif'

Methods

get()

Get MIME type from file extension

// Single extension
Mime::get('pdf');   // 'application/pdf'
Mime::get('jpg');   // 'image/jpeg'
Mime::get('mp4');   // 'video/mp4'
Mime::get('xlsx');  // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

// Unknown extension
Mime::get('unknown');  // null

// Multiple extensions
Mime::get(['jpg', 'png', 'gif']);
// ['jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif']

Parameters:

  • $exts (string|array): File extension or array of extensions

Returns:

  • string: MIME type (if string extension passed)
  • array: Array of extension => MIME type (if array passed)
  • null: If extension not found

check()

Check if MIME type matches accepted extensions

// Check if MIME type is an allowed image
$isAllowed = Mime::check(['jpg', 'png', 'gif'], 'image/jpeg');  // true
$isAllowed = Mime::check(['jpg', 'png'], 'application/pdf');    // false

// Use with file upload validation
$allowedTypes = ['jpg', 'png', 'gif'];
$uploadedMime = $_FILES['image']['type'];

if (!Mime::check($allowedTypes, $uploadedMime)) {
    throw new Exception('Invalid file type');
}

Parameters:

  • $typies (array): Array of allowed file extensions
  • $mime (string): MIME type to check

Returns:

  • bool: true if MIME type matches any extension in array, false otherwise

getAccept()

Generate accept string for HTML input type="file"

$accept = Mime::getAccept(['jpg', 'png', 'gif']);
// 'image/jpeg,image/png,image/gif'

// Use with HTML form
echo '<input type="file" accept="' . Mime::getAccept(['jpg', 'png']) . '">';
// <input type="file" accept="image/jpeg,image/png">

Parameters:

  • $typies (array): Array of file extensions

Returns:

  • string: Comma-separated MIME types

Supported MIME Types

This class supports 900+ file extensions including:

Images

Extension MIME Type
jpg, jpeg image/jpeg
png image/png
gif image/gif
webp image/webp
svg image/svg+xml
ico image/x-icon
bmp image/bmp
tiff image/tiff

Documents

Extension MIME Type
pdf application/pdf
doc application/msword
docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
xls application/vnd.ms-excel
xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
ppt application/vnd.ms-powerpoint
pptx application/vnd.openxmlformats-officedocument.presentationml.presentation

Videos

Extension MIME Type
mp4 video/mp4
webm video/webm
avi video/x-msvideo
mov video/quicktime
flv video/x-flv

Audio

Extension MIME Type
mp3 audio/mpeg
wav audio/wav
ogg audio/ogg
m4a audio/mp4

Archives

Extension MIME Type
zip application/zip
rar application/x-rar-compressed
7z application/x-7z-compressed
tar application/x-tar
gz application/gzip

Examples

File Upload Validation

use Kotchasan\Mime;

$allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];
$file = $_FILES['avatar'];

// Check MIME type
if (!Mime::check($allowedTypes, $file['type'])) {
    die('Invalid file type. Only images allowed.');
}

// Proceed with upload
uploadFile($file);

HTML File Input with Accept

use Kotchasan\Mime;
use Kotchasan\Html;

$form = Html::form();

// Create file input for images
$imageAccept = Mime::getAccept(['jpg', 'jpeg', 'png', 'gif', 'webp']);
$form->add('file', [
    'id' => 'avatar',
    'accept' => $imageAccept,
    'label' => 'Upload Avatar'
]);

// Create file input for documents
$docAccept = Mime::getAccept(['pdf', 'doc', 'docx', 'xls', 'xlsx']);
$form->add('file', [
    'id' => 'document',
    'accept' => $docAccept,
    'label' => 'Upload Document'
]);

Set Content-Type Header

use Kotchasan\Mime;

$filename = 'report.pdf';
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$mime = Mime::get($ext);

if ($mime) {
    header('Content-Type: ' . $mime);
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    readfile($filepath);
}

Dynamic File Type Detection

use Kotchasan\Mime;

function getFileType($filename) {
    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    $mime = Mime::get($ext);

    if (!$mime) {
        return 'unknown';
    }

    if (strpos($mime, 'image/') === 0) {
        return 'image';
    } elseif (strpos($mime, 'video/') === 0) {
        return 'video';
    } elseif (strpos($mime, 'audio/') === 0) {
        return 'audio';
    } elseif (in_array($ext, ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'])) {
        return 'document';
    } else {
        return 'file';
    }
}

echo getFileType('photo.jpg');     // 'image'
echo getFileType('video.mp4');     // 'video'
echo getFileType('report.pdf');    // 'document'
  • File - File operations
  • Image - Image processing
  • Upload - File upload handling