API / Minn\Runtime / FileTypeCheck

FileTypeCheck final class

Minn\Runtime\FileTypeCheck · public/minn/src/Minn/Runtime/FileTypeCheck.php · 77 lines

A file's type from its content as much as its name, as the reference's wp_check_filetype_and_ext decides it (probe upload-filters): an image the server can measure is what its bytes say, and a name with the wrong image extension is corrected (a.png holding a JPEG becomes a.jpg); anything claiming to be such an image and not being one has no type; any other type must match what the content is, plain text standing for a few text types and an office or archive container for the document types; and the type must be one the site allows.

Constants and properties

constIMAGES = array ( 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/tiff' => 'tif', 'image/webp' => 'webp', 'image/avif' => 'avif', 'image/heic' => 'heic', )

The image types measured by their bytes, and the extension each takes.

privateTEXT_TYPES = array ( 0 => 'text/plain', 1 => 'text/csv', 2 => 'application/csv', 3 => 'text/richtext', 4 => 'text/tsv', 5 => 'text/vtt', )
privateCONTAINERS = array ( 0 => 'application/octet-stream', 1 => 'application/encrypted', 2 => 'application/CDFV2-encrypted', 3 => 'application/zip', )

Public

staticcheck(string $file, string $filename, ?array $mimes, array $images): array#

ext, type, proper_filename, and the content's own type (real_mime).

$mimesarray<string, string>|null
$imagesarray<string, string>getimagesize_mimes_to_exts
@return array{ext: string|false, type: string|false, proper_filename: string|false, real_mime: string|false}

Internals 2

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privateimageMime(string $file): string|false#

What an image's header says it is (Media\ImageMime), or false when it is not an image.

privatecontentFits(string $type, string $real): bool#

Whether content of the real type may carry a name of the given type.