PHP 7.3 can now detect it properly using finfo_file()
. The fileinfo PHP extension uses bundled libmagic and seems the library already detects .docx files correctly in all currently supported PHP versions (7.4, 8.0, 8.1).
Running
finfo_file(finfo_open(FILEINFO_MIME_TYPE), 'test.docx');
now returns
application/vnd.openxmlformats-officedocument.wordprocessingml.document
You can see the result of the same function call on older PHP versions here https://3v4l.org/uSqkR - notice the change on 7.3. The example is using finfo_buffer()
and Base64-encoded file so that I can have the file "inlined" in the PHP code.
If the correct type is not detected, it's possible you may be using (even unknowingly) a custom "magic" database which does not support the type. You can specify the database as an extra parameter to finfo_open()
, for example
finfo_open(FILEINFO_MIME_TYPE, '/etc/magic.mime');
If the code you're using is doing that, and you're using PHP 7.3 or newer, remove the /etc/magic.mime
parameter.
The database can also be specified using the MAGIC
environment variable, check you have it unset with for example getenv('MAGIC')
or in phpinfo()
output. If that's the case you can remove the variable wherever is set, or unset it in your PHP code (putenv('MAGIC')
) before using finfo_open()
:
putenv('MAGIC');
finfo_file(finfo_open(FILEINFO_MIME_TYPE), 'test.docx');
zip
. If you want to know the type/format of the content, there is no way around into looking in it. – Fanionapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
andapplication/zip
for the same file on different servers - Debian and Centos respectively. This makes Laravel's validation for docx fail on the latter and work fine on the former. So be careful, test in the environment you deploy your code to. – Faitour