检查图像格式时出现in_array错误


error with in_array for checking the format of Images

我已经看到了其他问题的答案,所以我分离了爆炸函数,但仍然给出以下错误。我已经粘贴了文件上传的全部代码

警告:爆炸()期望参数2为字符串,数组在C:'xampp'htdocs'hiddenprocess.php第22行

警告:end()期望参数1为array, null,在C:'xampp'htdocs'hiddenprocess.php第25行

代码如下:

    <?php


    if (empty($_FILES['Upload_Property_Images']['name']))
    {
        echo 'You have''nt Entered Value for upload field'; 
    }
    else
    {

foreach($_FILES as $file) 
{
 // Allowed file types
 $whitelist = array("jpg","png");
 $temp = explode('.', $file['name']);
  // Match uploaded file extension
  if (in_array(end($temp), $whitelist )) 
  {
       // Count total uploads
       if (count($_FILES['Upload_Property_Images']['name'])==6) 
       {
         // Code for uploading here
          echo 'files uploaded successfully';
       } 
       else 
       {
       // Count error 
         echo 'You''r Only Allowed Six Images';
       }
  } 

  else 
  {
       // File extension error
       echo 'Your Only Allowed JPG and PNG';
  }

 }
}
?>

这是您的代码的工作版本。我一共做了四处修改,在注释中用**

标记
if (empty($_FILES['Upload_Property_Images']['name']))
{
    echo 'You have''nt Entered Value for upload field';
    exit(); 
}
else
{
    foreach($_FILES['Upload_Property_Images']['name'] as $file) // **CHANGED
    {
        if ($file) // **ADDED
        { 
            // Allowed file types
            $whitelist = array("jpg","png");
            $temp = explode('.', $file); // **CHANGED
            // Match uploaded file extension
            if (in_array(end($temp), $whitelist )) 
            {
                // Count total uploads
                if (count($_FILES) <= 6) // **CHANGED
                {
                    // Code for uploading here
                    echo 'files uploaded successfully';
                } 
                else 
                {
                    // Count error 
                    echo 'Your Only Allowed Six Images';
                }
            } 
            else 
            {
               // File extension error
               echo 'Your Only Allowed JPG and PNG';
            }
        }
    }
}

我认为应该是:

foreach($_FILES['Upload_Property_Images']['name'] as $file) {
    ...
    $temp = explode('.', $file);   // not $file['name']
    ...
}

验证图像的函数示例:

function validate()
{
    $errors = array();
    foreach($_FILES['Upload_Property_Images']['name'] as $file) // **CHANGED
    {
        if ($file) // **ADDED
        { 
            // Allowed file types
            $whitelist = array("jpg","png");
            $temp = explode('.', $file); // **CHANGED
            // Match uploaded file extension
            if (in_array(end($temp), $whitelist )) 
            {
                // Count total uploads
                if (count($_FILES) > 6) // **CHANGED
                {
                    // Count error 
                    $errors[] = 'Your Only Allowed Six Images';
                }
            } 
            else 
            {
               // File extension error
               $errors[] = 'Your Only Allowed JPG and PNG';
            }
        }
    }
    if (empty($errors))
    {
        return TRUE;
    }
    else
    {
        return array_unique($errors);
    }
}
$validation = validate();
if ($validation === TRUE)
{
    // uplaod images here
}
else
{
    print_r($validation);
}