Work-around libmagic failing for 1-byte files
authorAlexander Ebert <ebert@woltlab.com>
Tue, 2 Jan 2018 12:59:31 +0000 (13:59 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 2 Jan 2018 12:59:31 +0000 (13:59 +0100)
Causes the error message "finfo::file(): Failed identify data 0:(null)"
in older libmagic/PHP versions, mostly in the ancient PHP 5.x tree.

wcfsetup/install/files/lib/util/FileUtil.class.php

index 43c90e58b244acc025f0758dc312add2ff265be5..4920aab378e2d69d1a8b18ddd1053c6d56253451 100644 (file)
@@ -510,7 +510,10 @@ final class FileUtil {
                        self::$finfo = new \finfo(FILEINFO_MIME_TYPE);
                }
                
-               return self::$finfo->file($filename) ?: 'application/octet-stream';
+               // \finfo->file() can fail for files that contain only 1 byte, because libmagic expects at least
+               // a few bytes in order to determine the type. See https://bugs.php.net/bug.php?id=64684
+               $mimeType = @self::$finfo->file($filename);
+               return $mimeType ?: 'application/octet-stream';
        }
        
        /**