Core\Mime::detect
Core\Mime::detect(Core\Bytes::Source $data): Core\Mime::TypeCore\Mime::detect( Core\Bytes::Source $data, ): Core\Mime::Type
Core\Mime::detect finds out what kind of file some data is, such as a PNG image, a PDF or a ZIP archive. It reads only the first 12 bytes, where most file formats write a fixed signature.
You can pass bytes, an open Core\IO\File, an uploaded Core\Request\Part or the request body stream. A file keeps its position. An upload is not used up, so saveTo still writes the whole file after detect.
The result is a case of the enum Core\Mime::Type, such as Core\Mime::Type::Png. When no signature matches, the result is Core\Mime::Type::Unknown. Text formats such as JSON, CSV and HTML have no signature, so they are always Unknown.
detect has no parameter for a file name. Anybody can call a program photo.png, so only the bytes decide the answer.
Good to know: the result does not make the data safe. Bytes from a request are still tainted (they came from outside the program) after detect read them.
The examples below show checking some bytes, then a file name that is not a type, then an upload form that accepts only images and saves them.
Parameters
$dataCore\Bytes::SourceWhat to check: abytesvalue, an openCore\IO\File, an uploadedCore\Request\PartorCore\Request::bodyStream(). Only the first 12 bytes are read. A file keeps its position. An upload is not used up, so you can still save it or read it after this call.
Return value
Returns Core\Mime::Type. The case for the kind of file. It is Core\Mime::Type::Unknown when no format matches, and for every text format. The result does not make the data safe. Bytes detected as image/png are still tainted, because only the first bytes were checked.
Errors
LogicErrorThe part is not the one thefiles()loop is on. Or a loop,readAll()orsaveTo()has already started to read the upload, so its first bytes are gone. Calldetectbefore them.IOErrorThe file or the connection failed while the first bytes were read.
Examples
Is it a png
<?nvs// Shows how to check what kind of file some bytes are.
// These are the first eight bytes of every PNG image.bytes $upload = Core\Encoding::fromHex('89504e470d0a1a0a');
Core\Mime::Type $type = Core\Mime::detect($upload);// This prints "a PNG image".if ($type == Core\Mime::Type::Png) { echo "a PNG image\n";} else { echo "not a PNG image\n";}a PNG imageA name is not a type
<?nvs// Shows that `detect` reads the bytes, and not what a file is called.
// These bytes are the text "holiday.png". They are not an image.bytes $text = 'holiday.png' as bytes;// This prints "true".echo Core\Mime::detect($text) == Core\Mime::Type::Unknown ? 'true' : 'false', "\n";
// These bytes start a PDF document, whatever name the file had.bytes $pdf = "%PDF-1.7\n" as bytes;// This prints "true".echo Core\Mime::detect($pdf) == Core\Mime::Type::Pdf ? 'true' : 'false', "\n";truetrueAccept only images
<?nvs// Shows how an upload form can accept images and reject every other kind of file.
class Avatar{ public static function accepts(Core\Request\Part $upload): bool { Core\Mime::Type $type = Core\Mime::detect($upload); return $type == Core\Mime::Type::Png || $type == Core\Mime::Type::Jpeg || $type == Core\Mime::Type::Webp; }}
string $folder = Core\IO::createTemporaryDirectory();foreach (Core\Request::files() as Core\Request\Part $part) { // The gzip file is rejected, because it is not one of the three image types. if (!Avatar::accepts($part)) { echo $part->filename(), ": rejected\n"; continue; } // `detect` reads only the first bytes, so `saveTo` still writes the whole file. string $path = Core\IO::within($folder, $part->filename()); $part->saveTo($path, { max: 2000000 }); echo $part->filename(), ': accepted, ', Core\IO::size($path), " bytes saved\n";}me.png: accepted, 252 bytes savedme.jpg: accepted, 672 bytes savedphotos.tar.gz: rejected