如何通过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
}
}
答案 0 :(得分:0)
你需要构建一个数组来将数据插入到表中,我假设你有两个名为name
和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);
}
}