PHP to determine the file upload image Format method introduction

Source: Internet
Author: User
This article mainly introduces the PHP judgment file upload image format of the relevant information, hope that through this article can help you to achieve such a method, very reference value, the need for friends can refer to the next

PHP to determine the file upload image format examples of detailed

Determine the file picture type,


$type  = $_files[' image ' [' tmp_name '];//filename//$type  = $this->getimagetype ($type);  $filetype = [' jpg ', ' jpeg ', ' gif ', ' BMP ', ' PNG ']; if (! In_array ($type, $filetype)) {   return ' is not a picture type ';}

If the user modifies the file suffix to png jpeg, etc. can not be satisfied, check the data to solve the problem is to use the binary flow information to determine the file, if you happen to encounter such problems may wish to try:


//* determine whether the image Upload format is a return file suffix public function getimagetype ($filename) {$file =  fopen ($filename, ' RB '); $bin = Fread ($file, 2);  Read-only 2-byte fclose ($file);  $strInfo = @unpack (' C2chars ', $bin);  $typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);  DD ($typeCode);  $fileType = ";    Switch ($typeCode) {case 255216: $fileType = ' jpg ';   Break    Case 7173: $fileType = ' gif ';   Break    Case 6677: $fileType = ' bmp ';   Break    Case 13780: $fileType = ' png ';   Break  Default: $fileType = ' upload only image Type format ';  }//if ($strInfo [' chars1 ']== '-1 ' and $strInfo [' chars2 ']== ' -40 ') return ' jpg ';  if ($strInfo [' chars1 ']== ' -119 ' and $strInfo [' chars2 ']== ') return ' PNG '; return $fileType; }
Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.