Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中检查来自数组的上载文件类型。_Php_Arrays_File Upload_Foreach_File Type - Fatal编程技术网

在PHP中检查来自数组的上载文件类型。

在PHP中检查来自数组的上载文件类型。,php,arrays,file-upload,foreach,file-type,Php,Arrays,File Upload,Foreach,File Type,如何检查文件扩展名和mime类型是否在数组中这是我当前拥有的代码 $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','imag

如何检查文件扩展名和mime类型是否在数组中这是我当前拥有的代码

$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循环

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;
        }  
问题是,如果文件类型不是数组中的所有类型(这是不可能的),我会得到一个错误。如果上传文件位于数组中,则不会触发错误消息

if (!in_array($_FILES['upload_project_thum']['type'], $upload_permitted_types)){

   exit("Unsupported file type");
}
这应该从类型和扩展中寻找合适的字符串

另一种方法是修改循环,如下所示:

$is_allowed = false;
foreach ($upload_permitted_types as $image_type) {
    $type = explode(":", $image_type);
    if (($type[0] == $_FILES['upload_project_thum']['type']) && ($type[1] == $upload_project_thum_ext ) ) {
        $is_allowed = true;
        break;
    }
}

if( !$is_allowed ) {
        $errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail'. $type[1] . " Type: ". $type[0];
        $errflag = true;
}

我现在的做法是:

    $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;
}

这样做的好处是,它允许.gif文件具有jpeg的mime格式。因此,它不会强制矿山和扩展匹配,但会确保它们都是图像类型

我喜欢获取文件数组,然后使用foreach循环读取可能的条件

$is_error = TRUE;

$allowFileTypes = array(
            "image/png","image/jpg","image/jpeg"
        );

$UserBaseFolder = 'userfiles/'.$_SESSION['user_id'];
            if(!file_exists($UserBaseFolder)) {
            mkdir("userfiles/".$_SESSION['user_id']."/");
        } 



    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { 
        foreach($_FILES as $dat => $f) {
                if($f['size'] == "0") {
                    $msg .= '<p><span class="alert alert-danger">You must upload a file.</span></p>';
                    $is_error = FALSE;
                }

                if($f['size'] > "2000000") {
                    $msg .= '<p><span class="alert alert-danger">Your file size is too big.</span></p>';
                    $is_error = FALSE;
                }


                if(!in_array($f['type'],$allowFileTypes)){
                    $msg .= '<p><span class="alert alert-danger">Your file has invalid format. Please try again...</span></p>';
                    $is_error = FALSE;
                } else {
                $filepath = $UserBaseFolder.'/'.time().'-'.$f['name'];
                move_uploaded_file($f["tmp_name"], $filepath);
                }
$is_error=TRUE;
$allowFileTypes=数组(
“图像/png”、“图像/jpg”、“图像/jpeg”
);
$UserBaseFolder='userfiles/'。$\u会话['user\u id'];
如果(!file_存在($UserBaseFolder)){
mkdir(“userfiles/”$\u SESSION['user\u id']。“/”);
} 
如果($_FILES['file']['error']==UPLOAD\u ERR\u OK){
foreach($\u文件为$dat=>$f){
如果($f['size']=“0”){
$msg.='您必须上载文件。

; $is_error=FALSE; } 如果($f['size']>“2000000”){ $msg.='您的文件太大。

'; $is_error=FALSE; } if(!in_数组($f['type',$allowFileTypes)){ $msg.='您的文件格式无效。请重试…

; $is_error=FALSE; }否则{ $filepath=$UserBaseFolder.'/'.time().-'.$f['name']; 移动上传的文件($f[“tmp\U名称”],$filepath); }

return$msg;

这不只是检查mime类型是否正确,因此我需要将数组更改为只包含mime类型。另外,$\u文件['upload\u project\u thum']是否必须是$\u文件['upload\u project\u thum'][type]。我还在学习,所以我只是问问。谢谢你的帮助!编辑了答案。如果你想检查扩展,那么in_array()的第一个参数将是你上传文件的扩展名,第二个将是所有允许扩展名的数组。你是说有两个数组,一个用于扩展名,一个用于mime类型?然后使用两个in_数组?我可以看到这是如何工作的。谢谢你的帮助。谢谢,这似乎有效。我使用的是你的第一种方法。你的第二种方法是什么其他人也提出了建议,我相信它会起作用,但这只是更多的代码行。感谢您的帮助!PHP不是只是将扩展映射到
$\u文件中的mime类型吗?$allowFileTypes=array(“image/png”、“image/jpg”、“image/jpg”);
$is_error = TRUE;

$allowFileTypes = array(
            "image/png","image/jpg","image/jpeg"
        );

$UserBaseFolder = 'userfiles/'.$_SESSION['user_id'];
            if(!file_exists($UserBaseFolder)) {
            mkdir("userfiles/".$_SESSION['user_id']."/");
        } 



    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { 
        foreach($_FILES as $dat => $f) {
                if($f['size'] == "0") {
                    $msg .= '<p><span class="alert alert-danger">You must upload a file.</span></p>';
                    $is_error = FALSE;
                }

                if($f['size'] > "2000000") {
                    $msg .= '<p><span class="alert alert-danger">Your file size is too big.</span></p>';
                    $is_error = FALSE;
                }


                if(!in_array($f['type'],$allowFileTypes)){
                    $msg .= '<p><span class="alert alert-danger">Your file has invalid format. Please try again...</span></p>';
                    $is_error = FALSE;
                } else {
                $filepath = $UserBaseFolder.'/'.time().'-'.$f['name'];
                move_uploaded_file($f["tmp_name"], $filepath);
                }