php jquery clone输入字段在同一列

时间:2016-10-25 09:12:17

标签: javascript php jquery mysql

我正在创建一个表单。

表单中有几种字段类型。

其中我有一个可以填充的文件上传框。

用户可以在文件上传输入上附加多个文档。

  

提交文件名时应保存在同一列的db中   以逗号分隔。

对于单个文件我知道怎么做,但是因为它被克隆我很困惑

<span class="button btn-primary"> Choose File </span>
<input type="file" class="gui-file required" name="docs[]" onChange="document.getElementById('orderupload1').value = this.value;">
<input type="text" class="gui-input" name="orderupload1" id="orderupload1" placeholder="Attach Domcuments Here..." readonly>

使用上面的代码和一些JS,表单和字段填充效果很好。

现在的问题是如何在同一列中插入用逗号分隔的每个填充的字段文件名。

我已经为单场做了如下:

$docs=$_FILES['docs[]']['name'];

$temp_doc_name=$_FILES['docs[$i]']['tmp_name'];

if($docs!=''){


  if ($temp_doc_name != ""){

    $newFilePath1 = "uploadFiles/" . $docs;


    if(move_uploaded_file($temp_doc_name, $newFilePath1)) {


    }
  }
    }

$sql = "insert into students (documents) values(LAST_INSERT_ID(), '$docs')";

2 个答案:

答案 0 :(得分:1)

检查以下演示并相应地替换您的代码。

<强> HTML

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Multiple File Ppload with PHP</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  <input type="submit" value="Upload!" />
</form>
</body>
</html>

PHP代码

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
}

答案 1 :(得分:1)

使用foreach替换代码,如下所示。

foreach ($_FILES['docs']['name'] as $key => $val) {
   if (move_uploaded_file($_FILES['docs']['tmp_name'][$key], $newFilePath1)) {
      /* FILE IS UPLOADED SUCCESSFULLY */
   }
}