我正忙于编写一个PHP文件,以便安全地上传图像,如.jpg、.jpeg、.png、.bmp和.gif
这是代码:
$realname = $_FILES['userfile']['name'];
if(!preg_match("/('.jpg|'.png|'.gif|'.bmp|'.jpeg)$/i",$realname)) {
die();
}
有没有一种方法可以绕过这个检查来上传.php文件?我听说过php%00.jpg文件的技巧,但它是安全的。有人知道其他方法吗?还是上面的代码是安全的?
要回答这个问题:是的。
但你很幸运,我刚刚写了这个:
function checkImageFileIsImage($filepath) {
$type = exif_imagetype($filepath);
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3 // [] png
);
if (!in_array($type, $allowedTypes)) {
return false;
}
return true;
}
看看http://php.net/manual/en/function.exif-imagetype.php了解更多文件类型。