C# 檢測真實的檔案型別函式
private bool IsAllowedExtension(HttpPostedFile hifile)
{
bool ret = false;
System.IO.FileStream fs = new System.IO.FileStream(hifile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
return false;
}
r.Close();
fs.Close();
/*副檔名說明
*7173 gif
*255216 jpg
*13780 png
*6677 bmp
*239187 txt,aspx,asp,sql
*208207 xls.doc.ppt
*6063 xml
*6033 htm,html
*4742 js
*8075 xlsx,zip,pptx,mmap,zip
*8297 rar
*01 accdb,mdb
*7790 exe,dll
*5666 psd
*255254 rdp
*10056 bt種子
*64101 bat
*/
String[] fileType = { "255216", "7173", "6677", "13780", "8297", "5549", "870", "87111", "8075" };
for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
ret = true;
break;
}
}
return ret;
}
{
bool ret = false;
System.IO.FileStream fs = new System.IO.FileStream(hifile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
return false;
}
r.Close();
fs.Close();
/*副檔名說明
*7173 gif
*255216 jpg
*13780 png
*6677 bmp
*239187 txt,aspx,asp,sql
*208207 xls.doc.ppt
*6063 xml
*6033 htm,html
*4742 js
*8075 xlsx,zip,pptx,mmap,zip
*8297 rar
*01 accdb,mdb
*7790 exe,dll
*5666 psd
*255254 rdp
*10056 bt種子
*64101 bat
*/
String[] fileType = { "255216", "7173", "6677", "13780", "8297", "5549", "870", "87111", "8075" };
for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
ret = true;
break;
}
}
return ret;
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-536508/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 檢測真實的檔案型別函式型別函式
- 檔案檢測函式 feof, ferror,clearerr C庫函式函式Error
- 檔案上傳漏洞防範-檔案型別檢測型別
- python-magic:檢測檔案的MIME型別Python型別
- [PY3]——函式——函式註解 | 實現型別檢查功能函式型別
- js檢測上傳檔案型別程式碼例項JS型別
- linux檢視檔案型別Linux型別
- 檢視檔案系統型別型別
- 人臉檢測的harr檢測函式函式
- js檢測上傳檔案大小和型別例項程式碼JS型別
- js 型別檢測JS型別
- JavaScript型別檢測JavaScript型別
- 【JS】型別檢測JS型別
- JavaScript安全的型別檢測JavaScript型別
- TypeScript 函式型別TypeScript函式型別
- C#中解構函式,Close函式,Dispose函式的區別C#函式
- 我使用過的Linux命令之file - 檢測並顯示檔案型別Linux型別
- 關於onethink 目錄,檔案讀寫檢測函式中的問題函式
- 漏洞型別及檢測型別
- C++ 數學函式、標頭檔案及布林型別詳解C++函式型別
- C++基礎::函式、類、型別所在的標頭檔案 && 介面的介紹C++函式型別
- PHP實現強型別函式返回值PHP型別函式
- js檢測資料型別JS資料型別
- javascript 資料型別檢測JavaScript資料型別
- JavaScript的資料型別及其檢測JavaScript資料型別
- python的檔案型別Python型別
- .apk檔案的MIME型別APK型別
- java 中的檔案型別Java型別
- 胡說-JavaScript函式型別JavaScript函式型別
- PostgreSQL:字元——型別及函式SQL字元型別函式
- PHP:與型別有關的函式PHP型別函式
- C++14::lambda函式的型別C++函式型別
- Python 序列型別以及函式引數型別Python型別函式
- 人臉識別檢測專案實戰
- 第 8 節:函式-函式型別與作用域函式型別
- 函式引數 引數定義函式型別函式型別
- 人臉檢測識別,人臉檢測,人臉識別,離線檢測,C#原始碼C#原始碼
- C#的型別——值型別與引用型別C#型別