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 ;}}