好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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" , "& # x" , $string );  //     //过滤一些不安全字符       $string  =  addslashes ( $string );    }   }    return   $string ;  }    //实例:   $_COOKIE  = c_addslashes( $_COOKIE );  $_POST    = c_addslashes( $_POST );  $_GET    = c_addslashes( $_GET );    //在公共文件中加入   //开源代码phpfensi.com   if ( $_FILES ){     foreach (  $_FILES   as   $key  =>  $_value  )   {     $_FILES [ $key ][ 'type' ] = $_value [ 'type' ];     }    if ( substr ( $_FILES [ $key ][ 'type' ],0,6) != 'image/' )   {     exit ;   }  }  


查看更多关于php 判断文件上传类型与过滤不安全数据的详细内容...

  阅读:79次