I think I see what the issue is. The logic for determining MIME type is:
$type = $this->getMimeInternally();
// If mime_content_type returns application/zip or empty, perform magic byte detection
if ($type === 'application/zip' || empty($type)) {
return $this->detectUsingMagicBytes();
}
If I try to upload a zip file, which is detected by getMimeInternally()
as application/zip
based on its extension, it then goes into the magic byte detection logic:
private function detectUsingMagicBytes(): ?string
{
$handle = fopen($this->filePath, 'rb');
if (!$handle) {
return null;
}
$magicBytes = fread($handle, 4); // Read the first 4 bytes
fclose($handle);
foreach ($this->getMappings() as $mapping) {
foreach ($mapping['magicBytes'] as $bytes) {
if ($magicBytes === $bytes) {
// Additional checks for APK-specific files
if ($mapping['extension'] === 'apk' && !$this->isApk($this->filePath)) {
continue; // Not an APK, fallback to other mappings
}
return $mapping['mime'];
}
}
}
return null;
}
However, the mapping file is missing, so the method cannot find a suitable mapping for the zip file (my file starts with PK04x03x
which is standard for zip files) and returns null which leads to the exception I posted and ultimately an inability to upload.
In any case, it would be good to have a fail-safe against null, something like this:
$type = $this->getMimeInternally();
// If mime_content_type returns application/zip or empty, perform magic byte detection
if ($type === 'application/zip' || empty($type)) {
$magicBytesType = $this->detectUsingMagicBytes();
if ($magicBytesType) {
return $magicBytesType;
}
}
return $type;
The mappings between the magic bytes and the MIME type are expected to be in a file fof-upload.mime-mappings
but I can't find such a file. I checked that the above is a recent development by @IanM (I guess you're imorland?), is it possible that you forgot to include that file?