从PHP数组检查上传的文件类型。 [英] Check Upload file type from an array in PHP.
问题描述
我如何检查文件扩展名和MIME类型是一个数组,这是code我现在有。
How can I check if a file extension and mime type are in an array this is the code I currently have.
$upload_project_thum = $_FILES['upload_project_thum']['name'];
$upload_project_thum_ext = substr($upload_project_thum, strrpos($upload_project_thum, '.') + 1);
$upload_permitted_types= array('image/jpeg:jpg','image/pjpeg:jpg','image/gif:gif','image/png:png');
然后向下在那里我检查,如果该文件是一个有效的类型我有这样的foreach循环
Then down where i'm checking if the file is a valid type I have this foreach loop
foreach ($upload_permitted_types as $image_type) {
$type = explode(":", $image_type);
if (($type[0] != $_FILES['upload_project_thum']['type']) && ($upload_project_thum_ext != $type[1]) ) {
$errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail'. $type[1] . " Type: ". $type[0];
$errflag = true;
}
的问题是,如果文件类型不是所有阵列中的类型(这是不可能的)我得到一个错误。它的工作原理到如果上传文件是数组中的错误信息不会触发点。
The problem is that if the file type isn't ALL of the types in the array (which is impossible) I get an error. It works to the point where if the upload file is in the array that error message won't trigger.
推荐答案
我现在这样做的方法是:
The way I am doing this now is:
$upload_permitted_types['mime']= array('image/jpeg','image/gif','image/png');
$upload_permitted_types['ext']= array('jpeg','jpg','gif','png');
if(!in_array($_FILES['upload_project_thum']['type'],$upload_permitted_types['mime']) || !in_array($upload_project_thum_ext,$upload_permitted_types['ext'])
{
$errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail';
$errflag = true;
}
要这样做的好处是,它会允许与JPEG的MIME .gif文件。因此,它不会强制煤矿和扩展,以匹配但要确保它们都是图像类型。
The advantage to this is that it will allow a .gif file with a mime of jpeg. So it doesn't force the mine and the extension to match but does make sure they are both image types.
这篇关于从PHP数组检查上传的文件类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!