-1

如何通过 html5(javascript) 和 php(codeigniter) 在 mysql 数据库中上传多个图像。

我该怎么办,php(codeigniter)?(我不能改变javascript代码,请给我例子)

html:

<form action="#" method="post" enctype="multipart/form-data">
    <input name="name[]" type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)">
</form>

javascript如下:

function doClick() {
  var el = document.getElementById("fileElem");
  if (el) {
    el.click();
  }
}
function handleFiles(files) {
  var d = document.getElementById("fileList");
  if (!files.length) {
    d.innerHTML = "<p>No files selected!</p>";
  } else {
    var list = document.createElement("ul");
    d.appendChild(list);
    for (var i=0; i < files.length; i++) {
      var li = document.createElement("li");
      list.appendChild(li);

      var img = document.createElement("img");
      img.src = window.URL.createObjectURL(files[i]);;
      img.height = 60;
      img.onload = function() {
        window.URL.revokeObjectURL(this.src);
      }
      li.appendChild(img);

      var info = document.createElement("span");
      info.innerHTML = files[i].name + ": " + files[i].size + " bytes";
      li.appendChild(info);
    }
  }
}

我试过:

//$config['upload_path'] = './uploads/';
//$config['allowed_types'] = 'gif|jpg|png';
//$this->upload->initialize($config);
//$this->load->library('upload');

   for($i=0; $i<count($_FILES['name']); $i++){
        if ($_FILES['error'][$i] == 0) {
            $this->db->insert('table', $_FILES['name'][$i]); // this don't work
        }
    }
4

1 回答 1

0

您需要构建一个数组来将数据插入到表中,我假设您有两个名为nameand的字段data。请根据需要进行更正。

for($i=0; $i<count($_FILES['name']['name']); $i++){
        if ($_FILES['name']['error'][$i] == 0) {
            $dataf = file_get_contents($_FILES['name']['tmp_name'][$i]);
            $data = array('file_name' => $_FILES['name']['name'][$i], 'data' => $dataf);
            $this->db->insert('table', $data); 
        }
    } 
于 2013-07-13T20:03:19.877 回答