不知道有没有童鞋遇到过这种问题,好像有些手段把不是图片的文件伪装成图片,上传?

解决方案 »

  1.   

    何解?刚google了一下,对asp.net有类似的问题,不过没看明白
      

  2.   

    http://www.linuxpig.com/2010/04/php-detect-real-file-type-and-examples/
    一个例子
      

  3.   

    [Quote=引用 引用 4 楼 heyli 的回复:]http://www.linuxpig.com/2010/04/php-detect-real-file-type-and-examples/
    一个例子关注
      

  4.   

    参考 http://php.net/manual/en/reserved.variables.files.php
    Array
    (
        [file1] => Array
            (
                [name] => MyFile.txt (comes from the browser, so treat as tainted)
                [type] => text/plain  (not sure where it gets this from - assume the browser, so treat as tainted)
                [tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
                [error] => UPLOAD_ERR_OK  (= 0)
                [size] => 123   (the size in bytes)
            )    [file2] => Array
            (
                [name] => MyFile.jpg
                [type] => image/jpeg
                [tmp_name] => /tmp/php/php6hst32
                [error] => UPLOAD_ERR_OK
                [size] => 98174
            )
    )
    $_FILES['file2']['type'],如果不是image类型的那么就不会显示为 image/*的。
      

  5.   

    可以这样判断就方便多了
    if (FALSE!==strpos($type,'image')){//判断上传文件类型
    $size=$_FILES["file"]["size"];
    $tmp_name=$_FILES["file"]["tmp_name"];
    $images="../pic_upload/upload_file/$name";
    }
      

  6.   

    虽然有些缺陷不过目前来说还算可以接受
    function ftype($filename){
    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin);
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
    $fileType = '';
    switch($typeCode){
    case 7790:
    $fileType = 'exe';
    break;
    case 7784:
    $fileType = 'midi';
    break;
    case 8297:
    $fileType = 'rar';
    break;
    case 255216:
    $fileType = 'jpg';
    break;
    case 7173:
    $fileType = 'gif';
    break;
    case 6677:
    $fileType = 'bmp';
    break;
    case 13780:
    $fileType = 'png';
    break;
    default:
    $fileType = 'unknown';
    }
    return $fileType;
    }
    /*a  NUL-padded string(NUL填充字符串)
    A  SPACE-padded string(空格填充字符串)
    h  Hex string, low nibble first(十六进制字符串,先写低四位)
    H  Hex string, high nibble first(十六进制字符串,先读高四位)
    c signed char(有符号字符)
    C  unsigned char(无符号字符)
    s  signed short(有符号短整型) (always 16 bit, machine byte order)(总是16位,机字节顺序)
    S  unsigned short(无符号短整型) (always 16 bit, machine byte order)(总是16位,机字节顺序)
    n  unsigned short(无符号短整型) (always 16 bit, big endian byte order)(总是16位,大endian字节顺序)
    v  unsigned short(无符号短整型) (always 16 bit, little endian byte order)(总是16位,little endian字节顺序)
    i  signed integer(有符号整数) (machine dependent size and byte order)(取决于机器的大小和字节顺序)
    I  unsigned integer(无符号整型) (machine dependent size and byte order)(取决于机器的大小和字节顺序)
    l  signed long(有符号长整型) (always 32 bit, machine byte order)(总是32位,机字节顺序)
    L  unsigned long(无符号长整型) (always 32 bit, machine byte order)(总是32位,机字节顺序)
    N  unsigned long(无符号长整型) (always 32 bit, big endian byte order)(总是32位,大endian字节顺序)
    V  unsigned long(无符号长整型) (always 32 bit, little endian byte order)(总是32位,little endian字节顺序)
    f  float(单精度浮点型) (machine dependent size and representation)(取决于机器的大小和代表性)
    d  double(双精度浮点型) (machine dependent size and representation)(取决于机器的大小和代表性)
    x  NUL byte(NUL字节)
    X  Back up one byte(备份一个字节)
    @  NUL-fill to absolute position(NUL填充到绝对位置)
    */