我们可以在
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.