2014-05-07 40 views
0

我見過的其他問題的答案,所以我分開爆炸功能,但仍然給下面的錯誤。我粘貼了文件上傳的整個代碼。誤差in_array檢查圖像的格式

警告:爆炸()預計參數2爲字符串,陣列中的C中給出:\ XAMPP \ htdocs中\ hiddenprocess.php上線22

警告:端()預計參數1是數組,在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'; 
    } 


} 

} 

?> 
+2

是您的表單輸入名爲'type =「file」name =「Upload_Property_Images」'並且它是否包含'enctype =「multipart/form-data」'?或'name =「Upload_Property_Images []」' - 如果其中任何一個丟失或拼寫錯誤,您的代碼將失敗。 –

+0

雅它包含「ENCTYPE =」多部分/格式數據「也名字是這樣‘名稱=’Upload_Property_Images []」這是我的文件的代碼‘「 – user3610629

+0

另外,你有'method =」post「'? –

回答

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'; 
      } 
     } 
    } 
} 
+0

我改變,但現在得到這個錯誤「警告:發生爆炸()預計參數2爲字符串,陣列中的C中給出:\ XAMPP \ htdocs中\ hiddenprocess.php第21行 警告:端部( )預計參數1是數組,在C空給出:\ XAMPP \ htdocs中\ hiddenprocess.php上線24 你只允許JPG和PNG「 – user3610629

+0

等一下......讓我想一想。我知道答案與此有關,儘管... –

+0

@ user3610629嗯,我不知道,試着在'foreach'開頭添加'print_r($ file ['name'])'並告訴我你得到了什麼 –

0

我覺得應該是:爲驗證圖像功能

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

其工作,但如果我上傳兩個GIF圖片和四個JPG我得到成功上傳這個按摩'找你只允許JPG和PNG 找你只允許JPG和PNG 上傳的文件 文件上傳成功 文件上傳成功 文件成功' – user3610629

+0

@ user3610629請看我更新的答案。它解決了我們所遇到的所有問題,包括您剛剛提到的問題 –

+0

@ user3610629以及我無法看到您期望的輸出 – lulco

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

如何從輸出中刪除這些「Array([0] =>」,但是使用echo,但是發出'Array' – user3610629

+0

使用foreach($ validation爲$ error){echo $ error。'
';}而不是print_r($ validation); – lulco

+0

問題沒有解決,它顯示成功和錯誤按摩 – user3610629