Php obtains the Upload file type and obtains the file suffix.

Source: Internet
Author: User
The code is as follows: Copy code

// Obtain the image format, including jpg, png, and gif.
Function get_type ($ img_name) // Obtain the image file type
{
If (preg_match ("/. (jpg | jpeg | gif | png | bmp) $/I", $ img_name, $ matches )){
$ Type = strtolower ($ matches [1]);
} Else {
$ Type = "string ";
 }
Return $ type;
}

// Determine the Upload file type
$ Allowedextensions = array ("txt", "csv", "htm", "html", "xml ",
"Css tutorial", "doc", "xls", "rtf", "ppt", "pdf", "swf", "flv", "avi ",
"Wmv", "mov", "jpg", "jpeg", "gif", "png ");
Foreach ($ _ files as $ file ){
If ($ file ['tmp _ name']> ''){
If (! In_array (end (explode (".",
Strtolower ($ file ['name']),
$ Allowedextensions )){
Die ($ file ['name']. 'is an invalid file type! <Br/> '.
'<A href = "webpage effect: history. go (-1);"> '.
'& Lt; & lt go back </a> ');
      }  
}  
}  

// Optional file and image types

Function checktitle ($ filename ){
$ File = fopen ($ filename, "rb ");
$ Bin = fread ($ file, 2); // read-only 2 bytes
Fclose ($ file );
$ Strinfo = @ unpack ("c2chars", $ bin );
$ Typecode = intval ($ strinfo ['chars1']. $ strinfo ['chars2']);
$ Filetype = "";
Switch ($ typecode)
{
Case 7790:
$ Filetype = 'exe ';
Break;
Case 7784:
$ Filetype = 'midi ';
Break;
Case 8297:
$ Filetype = 'rar ';
Break;
Case 255216:
$ Filetype = 'jpg ';
Break;
Case 7173:
$ Filetype = 'GIF ';
Break;
Case 6677:
$ Filetype = 'bmp ';
Break;
Case 13780:
$ Filetype = 'PNG ';
Break;
Default:
$ Filetype = 'unknown '. $ typecode;
}
// Fix
If ($ strinfo ['chars1'] = '-1' & $ strinfo ['chars2'] ='-40 '){
Return 'jpg ';
}
If ($ strinfo ['chars1'] = '-119' & $ strinfo ['chars2'] = '80 '){
Return 'PNG ';
}
Return $ filetype;
}

 

?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.