Can we read the header information of a file in PHP to determine the type of file uploaded?.
I don’t want to rely on $_FILES['control_name_from_client']['type']. As we know that this property determines the file type by reading the extension of the file uploaded.
What if the user renames, say test.jpg -> test.xls. In that case, $_FILES['control_name_from_client']['type'] will show the type as application/vnd.ms-excel instead of image/jpeg. It is but natural this can create problems if a code has to be executed which reads the XLS file to fetch data for some processing.
Any suggestions please?
Try
finfo_file(). You have to call it passing the filepath. Example:You need the
Fileinfoextension. As PHP manual says: