Sample code for php to detect uploaded excel file type
Release: 2016-07-25 08:55:38
Original
1305 people have browsed it
-
-
- /**
- * Detect upload file type
- * Detect the excel file type of the uploaded file
- * @param array $file
- * @return bool $flag
- * @site bbs.it-home.org
- */
- private function detectUploadFileMIME($file) {
- // 1.through the file extension judgement 03 or 07
- $flag = 0;
- $file_array = explode ( ".", $file ["name"] );
- $file_extension = strtolower ( array_pop ( $file_array ) );
-
- // 2.through the binary content to detect the file
- switch ($file_extension) {
- case "xls" :
- // 2003 excel
- $fh = fopen ( $file ["tmp_name"], "rb" );
- $bin = fread ( $fh, 8 );
- fclose ( $fh );
- $strinfo = @unpack ( "C8chars", $bin );
- $typecode = "";
- foreach ( $strinfo as $num ) {
- $typecode .= dechex ( $num );
- }
- if ($typecode == "d0cf11e0a1b11ae1") {
- $flag = 1;
- }
- break;
- case "xlsx" :
- // 2007 excel
- $fh = fopen ( $file ["tmp_name"], "rb" );
- $bin = fread ( $fh, 4 );
- fclose ( $fh );
- $strinfo = @unpack ( "C4chars", $bin );
- $typecode = "";
- foreach ( $strinfo as $num ) {
- $typecode .= dechex ( $num );
- }
- echo $typecode;
- if ($typecode == "504b34") {
- $flag = 1;
- }
- break;
- }
-
- // 3.return the flag
- return $flag;
- }
复制代码
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31