Php prevents Image Trojans from being uploaded

Source: Internet
Author: User
Php prevents Image Trojans from being uploaded
 'No error occurs. the file is uploaded successfully. ', '1' =>' the uploaded file exceeds the limit of the upload_max_filesize option in php. ini. ', '2' =>' the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. ', '3' =>' only part of the file is uploaded. ', '4' =>' no file is uploaded. ', '5' =>' file failed to pass the security check. ', '6' =>' cannot find the temporary folder. ', '7' =>' file writing failed. ', '8' => 'File type not supported', '9' => 'the temporary file to be uploaded is lost. ',); // @ Start to execute the file upload public static function start ($ feild = 'file') {if (! Empty ($ _ FILES) {self: $ status = $ _ FILES [$ feild] ['error']; if (self: $ status> 0) return array ('status' => self: $ status, 'MSG '=> self: $ message [self: $ status]); self :: $ image = $ _ FILES [$ feild] ['tmp _ name']; self :: $ suffix = strtolower (strrchr ($ _ FILES [$ feild] ['name'], '. '); return array ('status' => self: _ upload (), 'path' => self: $ image, 'MSG' => self :: $ message [self ::$ status]);} else {return array ('status' => self: $ status, 'MSG '=> self :: $ message [self: $ status]) ;}// @ start private static function _ upload ($ path = '. /upload/') {date_default_timezone_set ('prc'); $ newFile = $ path. date ('Y/m/d/His '). rand (100,999 ). self: $ suffix; self: umkdir (dirname ($ newFile); if (is_uploaded_file (self: $ image) & move_uploaded_file (self: $ image, $ newFile) {self ::$ image = $ newFile; if (in_array (self ::$ suffix, self ::$ imageType) return self :: checkHex (); else return self: $ status = 0;} else {return self: $ status = 9 ;}/// @ private hexadecimal check private static function checkHex () {if (file_exists (self ::$ image) {$ resource = fopen (self ::$ image, 'RB'); $ fileSize = filesize (self :: $ image); fseek ($ resource, 0); if ($ fileSize> 512) {// Get the header and tail $ hexCode = bin2hex (fread ($ resource, 512 )); fseek ($ resource, $ fileSize-512); $ hexCode. = bin2hex (fread ($ resource, 512);} else {// retrieve all $ hexCode = bin2hex (fread ($ resource, $ fileSize ));} fclose ($ resource);/* matches <% () %> * // * matches in hexadecimal notation
 * // * Match the hexadecimal value

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.