- // Check the file type
- // By http://bbs.it-home.org
- Function checkFileType ($ filename ){
- // File header
- $ _ Typecode = array (
- '000000', // PDF
- '123', // .docx,.xlsx,.pptx,. potx,. vsdx,. odt
- '20140901', // .doc,.xls,.ppt,. sealing,. pot,. wps,. dps,. et
- );
- $ File = fopen ($ filename, "rb ");
- // Contents = stream_get_contents ($ file );
- // $ Contents = fread ($ file, filesize ($ filename ));
- $ Bin = fread ($ file, 2); // read-only 2 bytes
- Fclose ($ file );
- $ StrInfo = @ unpack ("C2chars", $ bin); // C is an unsigned integer. all searched online is c, which is a signed integer. Therefore, negative numbers are generated.
- $ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
- Exec ("file $ filename", $ output, $ return_var); // use the linux system command file to determine the type of the uploaded file, mainly to determine the txt and rtf file types
- $ Pattern = '/text,/'; // both the rtf and txt files are detected as text
- $ _ Count = preg_match ($ pattern, strrchr ($ output [0], ":");
- Echo $ typeCode;
- If (in_array ($ typeCode, $ _ typecode) | $ _ count = 1 ){
- Return true;
- } Else {
- Return false;
- }
- }
- ?>
|