實踐php檢測圖片木馬
- /**
- +------------------------------------------------------------------------------
- * Upload 檔案上傳類
- +------------------------------------------------------------------------------
- * @package Upload
- * @author nicegy
- * @version $Id: Upload.class.php 2014-4-11 19:00:23 nicegy $
- +------------------------------------------------------------------------------
- */
- class Upload {
-
- private static $image = null;
- private static $status = 0;
- private static $suffix = null;
- private static $imageType = array('.jpg', '.bmp','.gif','.png');
- private static $message = array(
- '0' => '沒有錯誤發生,檔案上傳成功。',
- '1' => '上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值。',
- '2' => '上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。',
- '3' => '檔案只有部分被上傳。',
- '4' => '沒有檔案上傳。',
- '5' => '未能通過安全檢查的檔案。',
- '6' => '找不到臨時檔案夾。',
- '7' => '檔案寫入失敗。',
- '8' => '檔案類型不支援',
- '9' => '上傳的臨時檔案丟失。',
- );
-
- //@ 開始執行檔案上傳
- 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]);
- }
- }
-
- //@ 私人 上傳開始
- 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;
- }
- }
-
- //@ 私人 16進位檢測 駭客
- private static function checkHex() {
- if (file_exists(self::$image)) {
- $resource = fopen(self::$image, 'rb');
- $fileSize = filesize(self::$image);
- fseek($resource, 0);
- if ($fileSize > 512) { // 取頭和尾
- $hexCode = bin2hex(fread($resource, 512));
- fseek($resource, $fileSize - 512);
- $hexCode .= bin2hex(fread($resource, 512));
- } else { // 取全部
- $hexCode = bin2hex(fread($resource, $fileSize));
- }
- fclose($resource);
- /* 匹配16進位中的 */
- /* 匹配16進位中的 */
- /* 匹配16進位中的
複製代碼
|