php 判断文件上传类型与过滤不安全数据
禁止上传除图片文件以外的文件,提示,不要获取文件扩展名来判断类型,这样是最不安全的,我们用$_FIlES['form']['type'].
这个可以读取文件内容来识别文件类型,但它能识别的有限,不过如果你用图片就足够了解.函数,过滤不安全字符,实例函数代码如下:
<?php
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);
//在公共文件中加入
//开源代码phprm.com
if ($_FILES) {
foreach ($_FILES as $key => $_value) {
$_FILES[$key]['type'] = $_value['type'];
}
if (substr($_FILES[$key]['type'], 0, 6) != 'image/') {
exit;
}
}文章地址:http://www.phprm.com/scxz/fs5180.html
转载随意^^请带上本文地址!