微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何读取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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐