使用限制上传图像

时间:2014-05-06 06:56:46

标签: php html

您好!我有文件上传我只想上传六张图片而不是更多只有六张图片使用PNG和JPG类型我写了下面的代码。

但是这样做会出现以下错误,请检查一下我的代码并找出错误。

  

“警告:preg_match()期望参数2为字符串,第21行的C:\ xampp \ htdocs \ hiddenprocess.php中给出的数组       您的图像必须是JPG或PNG并且等于六个图像       警告:preg_match()要求参数2为字符串,在第21行的C:\ xampp \ htdocs \ hiddenprocess.php中给出数组       您的图像必须是JPG或PNG并且等于六个图像“

HTML Code:

<form name="myWebForm" action="hiddenprocess.php" method="post"  enctype="multipart/form-data">

<input type="file" name="Upload_Property_Images[]" multiple="multiple"/>
<input type="submit" name="submit" value="Upload_Images" style="cursor:pointer;"/>

</form>

   Here is the PHP FILE UPLOAD CODE:

<?php

$imagename = $_FILES['Upload_Property_Images']['name'];
$imagetype = $_FILES["Upload_Property_Images"]['type'];


    if (empty($imagename))
    {
        echo 'You have\'nt Entered Value for upload field';
        exit(); 
    }

    else

    {

$whitelist = array(".jpg",".png");
                  foreach ($whitelist as $item)
                    {
                       if(preg_match("/$item\$/i", $imagename) && count($imagename ==6))
                          {

                           //code for uploading goes in here

                          }
                          else
                          {
                               echo 'Your Images Must Be 
                               -JPG OR PNG
                               -Only six images allowed';

                          }
                    }

    }



?>

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

// Process files one by one
foreach($_FILES as $file) {

 // Allowed file types
 $whitelist = array("jpg","png");

  // Match uploaded file extension
  if ( in_array(end(explode('.', $file['Upload_Property_Images']['name'])), $whitelist ) {

    // Count total uploads
    if (count($_FILES['Upload_Property_Images']) == 6) {
       // Code here
    } else {
       // Count error 
    }
  } else {
       // File extension error 
  }
}