我需要检查上传文件的类型,以确保它是一个图像。我想要做的是检查MIME文件类型(从$_FILE
),然后检查该扩展名。例如:检查文件类型?
function checkType($file){
$ext = pathinfo($_FILES[$file]['name'], PATHINFO_EXTENSION);
$mime = $_FILES[$file]['type'];
if($mime == "image/jpg" || $mime == "image/jpeg"){
if($ext == "jpg" || $ext == "jpeg"){
return true;
} else{
return false;
}
}
if($mime == "image/gif"){
if($ext == "gif"){
return true;
} else{
return false;
}
}
if($mime == "image/png"){
if($ext == "png"){
return true;
} else{
return false;
}
}
}
这是一个好方法吗?建议?
另外,在最终上传到服务器之前,如何获取图像的宽度?
非常感谢。我可以使用'getimagesize'作为图像的宽度吗? – user811348
@ user811348 - 当然可以!看看文档。上面的'$ imagedata'数组的第一个和第二个成员包含高度和宽度。 :) –
太棒了。但有一件事。它不工作。即使我只是将其简化为仅回显MIME类型,它也不会返回任何内容。 – user811348