Determine whether a file is an image file to upload

Method One: The image object is to determine whether the picture

///  <Summary> 
/// determines whether the image file
 ///  </ Summary> 
///  <param name = "path"> full path of the file </ param> 
///  <Returns> return results </ Returns> 
public Boolean isImage ( String path) 
{ 
the try 
{ 
 the System.Drawing.Image IMG = System.Drawing.Image.FromFile (path);
  return  to true ; 
} 
the catch (Exception E) 
{ 
 return  to false ; 
} 
}

The second method determines the file header

///  <Summary> 
/// The file type of the file header is determined uploaded
 ///  </ Summary> 
///  <param name = "filePath"> filePath is the full path to the file </ param> 
///  <Returns > returns true or to false </ returns> 
Private  BOOL IsPicture ( String filePath) 
{ 
the try 
{ 
 the FileStream FS = new new the FileStream (filePath, FileMode.Open, FileAccess.Read); 
 the BinaryReader Reader = new new the BinaryReader (FS);
  String fileClass;
  byte Buffer ; 
 Buffer =  reader.ReadByte ();
 fileClass = buffer.ToString();
 buffer = reader.ReadByte();
 fileClass += buffer.ToString();
 reader.Close();
 fs.Close();
 if (fileClass == "255216" || fileClass == "7173" || fileClass == "13780" || fileClass == "6677")
 //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 
 {
 return true;
 }
 else
 {
 return false;
 }
}
catch
{
 return false;
}
}
public enum FileExtension
{
    JPG = 255216,
    GIF = 7173,
    BMP = 6677,
    PNG = 13780,
    COM = 7790,
    EXE = 7790,
    DLL = 7790,
    RAR = 8297,
    ZIP = 8075,
    XML = 6063,
    HTML = 6033,
    ASPX = 239187,
    CS = 117115,
    JS = 119105,
    TXT = 210187,
    SQL = 255254,
    BAT = 64101,
    BTSEED = 10056,
    RDP = 255254,
    PSD = 5666,
    PDF = 3780,
    CHM = 7384,
    LOG = 70105,
    REG = 8269,
    HLP = 6395,
    DOC = 208207,
    XLS = 208207,
    DOCX = 208207,
    XLSX = 208207,
}

It is said that for the conventional method two modified Trojans effective, that is, to directly modify the extension, such as .jpg into the .asp this. Jpg but there is no effect on the kind of Trojan tool generates. We recommend you use the first good.

Guess you like

Origin www.cnblogs.com/Violety/p/11345910.html