Skip to content

Core\Mime::Type

What a run of bytes turned out to be, read from the bytes themselves. Core\Mime::detect answers with one of these cases, one per format it can recognise: images, audio and video, documents and archives.

Core\Mime::Type is declared in the class Core\Mime. The method detect returns it, and the method mediaType takes it as an argument.

A file name is never part of the answer. Whoever uploads a file chooses what to call it, so a type taken from the .png on the end of it is a type a stranger chose for you. Detection reads the first few bytes instead, which every one of these formats begins with.

Unknown is an ordinary answer and not a failure. Anything written as text — JSON, CSV, SVG, an HTML page — starts with whatever its author typed and has no first bytes to recognise, so Unknown is what you get for it, and your program has to handle it.

Good to know: a case has a spelling to send in a Content-Type header, and it only goes that way. A Content-Type a visitor sent you never becomes one of these cases.

Examples

What are these bytes

<?nvs
// `Core\Mime::detect` reads the first bytes of a file and returns its type.
// This program checks four files in the `data` folder beside it.
array<Core\IO\File> $arrived = [
'a photo from a phone' => Core\IO::open('data/photo.jpg', Core\IO::FileMode::Read),
'a screenshot' => Core\IO::open('data/screenshot.png', Core\IO::FileMode::Read),
'an invoice' => Core\IO::open('data/invoice.pdf', Core\IO::FileMode::Read),
'a spreadsheet' => Core\IO::open('data/sales.xlsx', Core\IO::FileMode::Read),
];
foreach ($arrived as string $what => Core\IO\File $file) {
// `detect` reads only the start of the file. It does not read the whole file.
Core\Mime::Type $found = Core\Mime::detect($file);
echo $what, ': ', Core\Mime::mediaType($found), "\n";
}
// The spreadsheet is a zip, and that is correct. A `.xlsx`, a `.docx` and a `.jar`
// file are all zip archives. Only the files inside the archive are different.
Output
a photo from a phone: image/jpeg
a screenshot: image/png
an invoice: application/pdf
a spreadsheet: application/zip

A name is not an answer

<?nvs
// The sender of a file chooses its name, so the name proves nothing. The
// bytes do. This file is called `holiday.png`, but it is a PDF.
string $name = 'holiday.png';
bytes $content = Core\Encoding::fromHex('255044462d312e370a25');
Core\Mime::Type $found = Core\Mime::detect($content);
echo $name, ' is really ', Core\Mime::mediaType($found), "\n";
// Text has no first bytes to recognise. A CSV file, a JSON document or a web
// page starts with whatever its author typed.
Core\Mime::Type $text = Core\Mime::detect("name,total\nORD-1042,19.99\n" as bytes);
echo 'the csv is ', Core\Mime::mediaType($text), "\n";
// So the result is `Unknown`. This means the bytes do not show their type.
// It is not an error.
echo ($text == Core\Mime::Type::Unknown) ? "nothing to read, and that is fine\n" : "named\n";
Output
holiday.png is really application/pdf
the csv is application/octet-stream
nothing to read, and that is fine

Only the uploads you want

<?nvs
// An upload form that accepts only a photo or a PDF. The check reads the bytes,
// not the file name. So `setup.png`, which is really a zip archive, is not accepted.
class Upload
{
public static function accepted(Core\Mime::Type $found): bool
{
return match ($found) {
Core\Mime::Type::Png => true,
Core\Mime::Type::Jpeg => true,
Core\Mime::Type::Pdf => true,
default => false,
};
}
}
array<string> $received = [
'receipt.pdf' => '255044462d312e37',
'logo.png' => '89504e470d0a1a0a',
'notes.txt' => '68656c6c6f2c2074686572650a',
'setup.png' => '504b03041400',
];
foreach ($received as string $name => string $firstBytes) {
Core\Mime::Type $found = Core\Mime::detect(Core\Encoding::fromHex($firstBytes));
echo $name, ': ', Upload::accepted($found) ? 'kept' : 'refused',
' (', Core\Mime::mediaType($found), ")\n";
}
Output
receipt.pdf: kept (application/pdf)
logo.png: kept (image/png)
notes.txt: refused (application/octet-stream)
setup.png: refused (application/zip)