2014-06-19 21 views
0

我有一個動態創建的字段,其中一個是文件輸入字段。驗證多個動態創建的字段

對於非動態創建的輸入字段,我使用它來驗證服務器端:

if(isset ($_POST['submitform'])) 
    { 
     $error = array(); 

     foreach($_POST as $key => $value) 
      { 
       $data[$key] = filter($value); 
      } 

     if(empty($data['randominputfield'])) 
      { 
       $error[] = "<p>Input something here</p>"; 
      } 

     $target = "path/to/folder/"; 

     $fileinput = ($_FILES['fileupload']['name']); 
     $filebasename = substr($fileinput, 0, strripos($fileinput, '.')); 
     $fileextension = substr($fileinput, strripos($fileinput, '.')); 
     $filesize = $_FILES["fileupload"]["size"]; 
     $fileformats = array('.png','.jpeg','.jpg','.gif'); 

     if (in_array($fileextension, $fileformats) && ($filesize <= 350000)) 
      { 
       // Rename file 
       $fileinputnewname = md5($filebasename) . $fileextension; 
       if (file_exists("path/to/folder/" . $fileinputnewname)) 
        { 
         $error[] = "<p>A file with this name already exists</p>"; 
        } 
       else 
        { 
         $filemove = move_uploaded_file($_FILES["fileupload"]["tmp_name"], "path/to/folder" . $fileinputnewname); 
        } 
      } 

     elseif (empty($filebasename)) 
      { 
       $error[] = "<p>Please select a file to upload</p>"; 
      } 

     elseif ($filesize > 350000) 
      { 
       $error[] = "<p>Files should not exceed 350kB</p>"; 
      } 
     else 
      { 
       $error[] = "<p>Only files with the extensions ". implode(', ',$fileformats)." are allowed</p>"; 
       unlink($_FILES["fileupload"]["tmp_name"]); 
      } 


     if(empty($error)) 
      { 
       //perform database insertion 
      } 
    } 

和精美的作品。

但現在我想給用戶添加字段無限多的機會(我做到了這一點使用jQuery),這樣他們可以在上傳一組以上的數據(即隨機輸入和文件)同時,我想驗證,然後繼續將每組數據存儲在數據庫中。有任何想法嗎?

+1

如此循環,雖然提交的文件\名 –

+0

@Dagon一個例子,也許? – user3754923

回答

0

我喜歡用這種方法來輸入一個動態的數字...

首先讓你的jQuery給每個數據在表格上設置這樣追加:

//On each click at the "plus" button append two inputs like these ones 
<input name="dynamicFields[fileDescription][]" placeholder="File description..."><br> 
<input type="file" name="dynamicFields[file][]"> 

的在服務器端,您將會獲得您的$_POST var兩個數組,其中一個帶有文件描述,另一個帶有文件。您可以使用此方法來組數據集:

public static function parseInputTable($inputTable) 
{ 
    $result = array(); 

    foreach ($inputTable as $columnName => $columnValues) { 
     foreach ($columnValues as $rowIndex => $columnValue) { 
      $result[$rowIndex][$columnName] = $columnValue; 
     } 
    } 

    return $result; 
} 

因此,例如:

var_dump(parseInputTable($_POST['dynamicFields'])); 

輸出

array(3) { 
    [0] => array(2) { 
    ['fileDescription'] => string(4) "foo1", 
    ['file'] => /*file 1*/ 
    }, 
    [1] => array(2) { 
    ['fileDescription'] => string(4) "foo2", 
    ['file'] => /*file 2*/ 
    }, 
    [2] => array(2) { 
    ['fileDescription'] => string(4) "foo3", 
    ['file'] => /*file 3*/ 
    } 
} 
+0

我真的不明白這一點。我試圖驗證它們,然後將它們插入到數據庫中。這就是我想要做的。 – user3754923

+0

@ user3754923我已經描述瞭如何處理表單中的多個動態字段以在PHP中解析它們。現在,您只需在'parseInputTable'返回中運行一個'foreach'並驗證每個數據集,然後只有在沒有發現錯誤的情況下才將它們添加到數據庫中。得到它了? – cvsguimaraes