Home  >  Article  >  Backend Development  >  php判断文件上传类型及过滤不安全数据的方法_PHP

php判断文件上传类型及过滤不安全数据的方法_PHP

WBOY
WBOYOriginal
2016-06-01 11:06:55923browse

本文实例讲述了php判断文件上传类型及过滤不安全数据的方法。分享给大家供大家参考。具体如下:

禁止上传除图片文件以外的文件,提示,不要获取文件扩展名来判断类型,这样是最不安全的,我们用$_FIlES['form']['type'].

这个可以读取文件内容来识别文件类型,但它能识别的有限,不过如果你用图片就足够了解.函数,过滤不安全字符,实例函数代码如下:

代码如下:

function s_addslashes($string, $force = 0) {
 if(!get_magic_quotes_gpc()) {
  if(is_array($string)) {
   foreach($string as $key => $val) {
    $string[$key] = s_addslashes($val, $force);
   }
  } else {
   $string=str_replace("","& # x",$string); //
 
//过滤一些不安全字符
   $string = addslashes($string);
  }
 }
 return $string;
}
 
//用法实例:
$_COOKIE = c_addslashes($_COOKIE);
$_POST   = c_addslashes($_POST);
$_GET   = c_addslashes($_GET);
 
//在公共文件中加入
if($_FILES){ 
 foreach( $_FILES as $key => $_value )
 {
  $_FILES[$key]['type'] =$_value['type'];  
 }
 if(substr($_FILES[$key]['type'],0,6) !='image/')
 {
  exit;
 }
}

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn