in_array错误,用于检查图像的格式

时间:2014-05-07 04:48:56

标签: php

我已经看到了其他问题的答案,所以我分开了爆炸功能,但仍然给出了以下错误。我已经粘贴了整个文件上传代码。

  

警告:explode()期望参数2为字符串,在第22行的C:\ xampp \ htdocs \ hiddenprocess.php中给出数组

警告:end()期望参数1为数组,在第25行的C:\ xampp \ htdocs \ hiddenprocess.php中给出null

以下是代码:

    <?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';
  }


 }

}

?>

3 个答案:

答案 0 :(得分:1)

这是您的代码的工作版本。我总共进行了四次更改,在**

的评论中进行了标记
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';
            }
        }
    }
}

答案 1 :(得分:0)

我认为应该是:

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

答案 2 :(得分:0)

验证图像的功能示例:

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