我想将多个图像添加到单个产品中,并将所有图片名称插入数据库。现在我只能将一个图像上传并存储到数据库中,但仍然无法找到上传多个图像的方法。
我不确定在哪里放置循环以及循环应包含的内容。
以下是我插入单张图片的代码:
控制器:
public function coba(){
$uploadPath = realpath('./assets/images/test/');
chmod($uploadPath, 0777);
$nama_array = array();
$count = count($_FILES["gambar"]["size"]);
foreach($_FILES as $k=>$v)
for($i = 0; $i <= $count-1; $i++) {
$nama = $v["name"][$i];
$config["upload_path"] = $uploadPath;
$config["allowed_types"] = "jpg|png|jpeg";
$config["overwrite"] = true;
$config["remove_spaces"] = true;
$config["max_size"] = 600;
$config["max_width"] = 800;
$config["max_height"] = 600;
$this->load->library("upload", $config);
$this->upload->do_upload("gambar[$i]");
$image_data = $this->upload->data();
$nama_array[] = $image_data["file_name"];
echo $nama . "<br>";
}
$names = implode(",", $nama_array);
print_r($nama_array);
echo "<br>" . $count . "<br>";
echo "sukses";
}
观点:
<html>
<head>
</head>
<body>
<?php
echo form_open_multipart("admin/coba");
echo "<p>";
echo form_upload("gambar");
echo "</p>";
echo form_submit("formSubmit", "Insert Product");
?>
</body>
</html>
带调整大小的库:
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Myupload {
public function uploadFile($field) {
//print_r($field);
$new_file = $field['files'];
$othimage = array();
$CI =& get_instance();
$config['upload_path'] = './assets/images/test/';
$config['allowed_types'] = 'png|gif|jpg|jpeg';
$config['max_size'] = '600';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->initialize($config);
$no = count($new_file['name'])-1;
$file= $_FILES;
for ($i=0; $i <= $no ; $i++)
{
//echo $file['files']['name'][$i]; exit;
$_FILES['files']['name']=$file['files']['name'][$i];
$_FILES['files']['type']= $file['files']['type'][$i];
$_FILES['files']['tmp_name']= $file['files']['tmp_name'][$i];
$_FILES['files']['error']= $file['files']['error'][$i];
$_FILES['files']['size']= $file['files']['size'][$i];
if ($CI->upload->do_upload('files'))
{
$other_images = $CI->upload->data();
$othimage[] = $other_images['file_name'];
$path = $other_images["full_path"];
$config1 = array(
"source_image" => $path,
"maintain_ratio" => false,
"width" => 640,
"height" => 480
);
echo $path . "<br>";
$CI->load->library("image_lib", $config1);
if(! $CI->image_lib->resize()){
echo "FAIL RESIZE";
}
}else{
$error = array('error' => $CI->upload->display_errors());
//print_r($error); exit;
}
}
return $othimage;
}
}
/* End of file Myupload.php */
答案 0 :(得分:1)
使用以下代码作为示例
public function add() {
$this->load->library(array('form_validation', 'myupload'));
//print_r($_FILES);exit;
if ($this->input->post('btnSubmit') == 'Submit') {
$this->form_validation->set_rules('title', 'Title', 'required|trim');
$this->form_validation->set_rules('content', 'Content', 'required|trim');
if ($this->form_validation->run() === TRUE) {
$insertData = array(
'title' => $this->input->post("title"),
'content' => $this->input->post("content"),
);
if ($portfolioId = $this->portfolio_model->add($insertData)) {
if (isset($_FILES) && $image = $this->myupload->uploadFile($_FILES, 'portfolio')) {
if (isset($image['error']) && $image['error']) {
echo $image['error'];
echo "Click <a href='" . base_url() . "admin/portfolio'>here</a> to go back";
exit;
} else {
foreach ($image as $img) {
$portfolioImage[] = array(
'images' => $img,
'portfolio_id' => $portfolioId
);
}
}
}
if ($portfolioId = $this->portfolio_model->add_images($portfolioImage)) {
$this->session->set_flashdata("success", "Portfolio added successfully");
} else {
$this->session->set_flashdata("error_message", "Portfolio was not added!!");
$this->session->set_flashdata('data', $this->input->post());
}
} else {
$this->session->set_flashdata("error_message", "Portfolio was not added!!");
$this->session->set_flashdata('data', $this->input->post());
}
} else {
$this->session->set_flashdata('error_message', validation_errors());
}
redirect("admin/portfolio");
}
}
并创建一个名为Myupload.php的库并粘贴到代码下面 更新图片调整大小的代码
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Myupload {
public function uploadFile($field, $folder) {
// print_r($field);
$new_file = $field['files'];
$CI =& get_instance();
$config['upload_path'] = './public/uploads/' . $folder . '/';
$config['allowed_types'] = 'png|gif|jpg|jpeg';
$config['max_size'] = '100000';
$config['max_width'] = '100000';
$config['max_height'] = '100000';
$CI->load->library('upload', $config);
$CI->load->library('image_lib');
$CI->upload->initialize($config);
$no = count($new_file['name'])-1;
$file= $_FILES;
for ($i=0; $i < $no ; $i++)
{
// echo $file['files']['name'][$i]; exit;
$_FILES['files']['name']=$file['files']['name'][$i];
$_FILES['files']['type']= $file['files']['type'][$i];
$_FILES['files']['tmp_name']= $file['files']['tmp_name'][$i];
$_FILES['files']['error']= $file['files']['error'][$i];
$_FILES['files']['size']= $file['files']['size'][$i];
if ($CI->upload->do_upload('files'))
{
$other_images = $CI->upload->data();
$othimage[] = $other_images['file_name'];
// print_r($other_images);exit;
//image resize
$config['image_library'] = 'gd2';
$config['source_image'] = $other_images['file_path'].$other_images['raw_name'].$other_images['file_ext'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 200;
$CI->image_lib->clear();
$CI->image_lib->initialize($config);
$CI->image_lib->resize();
//image resize end
}else{
$error = array('error' => $CI->upload->display_errors());
print_r($error); exit;
}
}
return $othimage;
}
}
/* End of file Myupload.php */
答案 1 :(得分:0)
我的型号代码
public function add($insertData) {
if ($this->db->insert('portfolio', $insertData))
return $this->db->insert_id(); //give last insert id
}
public function add_images($insertData) {
if ($this->db->insert_batch('portfolio_image', $insertData)) {
return TRUE;
}
}
答案 2 :(得分:0)
我已经用自己的方法破解了这个,但代码非常混乱,我甚至很难理解代码工作后,需要学习更多更简单有效的代码,
这是我的代码:
控制器:
$this->load->library("myupload", "form_validation");
$this->load->model("testModel");
$barangImage = array();
$hitung = 0;
if($this->input->post("formSubmit")) {
$this->form_validation->set_rules("nama", "Nama", "required|trim");
if($this->form_validation->run()) {
$insertData = array(
"nama" => $this->input->post("nama")
);
if($id = $this->testModel->add($insertData)) {
print_r($id);
$barangImage = array(
"barangid" => $id["id"]
);
if(isset($_FILES) && $image = $this->myupload->uploadFile($_FILES)) {
if(isset($image["error"]) && $image["error"]) {
echo $image["error"];
}else {
foreach($image as $img) {
++$hitung;
$barangImage[] = array(
"gambar" => $img,
"barangid" => $id
);
}
print_r($barangImage);
}
}
if($id = $this->testModel->add_images($barangImage, $hitung)) {
echo "SUCCESS !!!";
}else {
echo "FAIL INSERT IMAGES!!!";
}
}else {
echo "FAIL INSERT DATA NAMA";
}
}else {
echo "FAIL VALIDASI RUN";
}
}
型号:
public function add($newData){
$this->db->insert("cobabarang", $newData);
$nama = $newData["nama"];
$id = $this->db->query("SELECT id FROM cobabarang WHERE nama = \"$nama\"");
return $id->row_array();
//return $this->db->insert_id();
}
public function add_images($newImage, $banyakData) {
$id;
$gambar = null;
//$this->db->insert("cobagambar", $newImage);
if(count($newImage) != 1) {
$id = $newImage[0]["barangid"]["id"];
$gambar = null;
for($i = 0; $i < $banyakData; $i++){
if($banyakData-$i == 1) {
$gambar .= $newImage[$i]["gambar"];
}else {
$gambar .= $newImage[$i]["gambar"] . ",";
}
}
}else {
$id = $newImage["barangid"];
}
//$gambar = $newImage[1]["gambar"];
$this->db->query("INSERT INTO cobagambar(barangid, gambar) VALUES($id, \"$gambar\")");
}