Php implements the finfo_file method and binary stream method to accurately determine the file type without using the extension ],
This example describes how php can accurately determine the file type without passing the extension. We will share this with you for your reference. The details are as follows:
Method 1
Through phpfinfo_file()
$handle=finfo_open(FILEINFO_MIME_TYPE);//This function opens a magic database and returns its resource. $fileInfo=finfo_file($handle,'./test.txt');// Return information about a filefinfo_close($handle);print_r($fileInfo);echo '==========="\n"';
In addition
Finfo_buffer: Return information about a string buffer
Finfo_close: Close fileinfo resource
Mime_content_type: Detect MIME Content-type for a file (deprecated)
Method 2
Get file content through binary stream
Using fpw.fopen('test.txt ', 'R') // The actual image/png $ bin = fread ($ fp, 2); // read-only 2 bytes fclose ($ fp ); $ str_info = @ unpack ("C2chars", $ bin); // Unpack data from binary string $ type_code = intval ($ str_info ['chars1']. $ str_info ['chars2 ']); // Get the integer value of a variable $ file_type = "; switch ($ type_code) {case 7790: $ file_type = 'exe '; break; case 7784: $ file_type = 'midi '; break; case 8075: $ file_type = 'zip'; break; case 8297: $ file_type = 'rar'; break; case 255216: $ file_type = 'jpg '; break; case 7173: $ file_type = 'gif'; break; case 6677: $ file_type = 'bmp'; break; case 13780: $ file_type = 'png '; break; default: $ file_type = 'unknown'; break ;}