如何读取PHP上传文件的标题?

我们可以在 PHP中读取文件的头信息来确定上传文件类型吗?

我不想依赖$_FILES [‘control_name_from_client’] [‘type’].我们知道这个属性通过读取上传文件的扩展名来确定文件类型.

如果用户重命名,比如test.jpg – > TEST.XLS.在这种情况下,$_FILES [‘control_name_from_client’] [‘type’]将显示类型为application / vnd.ms-excel而不是image / jpeg.如果必须执行代码来读取XLS文件获取某些处理的数据,这很自然会产生问题.

有什么建议吗?

尝试 finfo_file().你必须通过文件路径调用它.例:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo,$_FILES['control_name_from_client']['tmp_name']);
finfo_close($finfo);

您需要Fileinfo扩展.正如PHP手册所说:

The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach the heuristics used do a very good job.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...