PHP checks whether the image is a standard image (prevent downloading the image from tampering)

Source: Internet
Author: User
PHP checks whether the image is a standard image (to prevent tampering with the image upload). during the project security check, it is found that the image upload in a series of projects only checks the suffix, as a result, 'image' containing some code can also be uploaded to the server, posing a major risk. I wrote a method to verify the correctness of the image. (This method cannot be completely verified, and the code added to the image source code cannot be judged. However, after image processing, such as watermarking, images containing code, when executed as php, will become invalid )? ** Determine the uploaded image. PHP checks whether the image is a standard image (prevents image tampering)

During the project security check, it was found that image uploading in a series of projects only checked the suffix, so that 'image' containing some code can also be uploaded to the server, posing a major risk. I wrote a method to verify the correctness of the image. (This method cannot be fully verified, and the code added to the image source code cannot be judged. However, after image processing, such as watermarking, images containing code will become invalid when executed as php)

?

/** Determine whether the uploaded image is a standard image * $ file $ FILES [''] the value * return normal image true; abnormal Image false; */function isimage ($ file) {if ($ file ["type"] = "image/gif ") {@ $ im = imagecreatefromgif ($ file ['tmp _ name']);} elseif ($ file ["type"] = "image/png" | $ file ["type"] = "image/x-png ") {@ $ im = imagecreatefrompng ($ file ['tmp _ name']);} else {@ $ im = imagecreatefromjpeg ($ file ['tmp _ name']);} if ($ im = false) {return false;} else {return true ;}}

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.