简体   繁体   English

Codeigniter多个图像上传并存储到数据库

[英]Codeigniter Multiple image upload and store to database

i'm want to build image gallery using codeigniter. 我想使用codeigniter建立图库。 But my doesn't work for multiple image upload. 但是我不能用于多张图片上传。 For now, my code just working for single image upload and store it into database. 现在,我的代码仅用于单个图像上载并将其存储到数据库中。

Here's my controller 这是我的控制器

function post()
{
    $config = array(
        array(
         'field'   => 'teknologi_title',
         'label'   => '*',
         'rules'   => 'required|min_length[5]'
      ),
        array(
         'field'   => 'sub_title',
         'label'   => '*',
         'rules'   => 'required|min_length[5]'
      ),
      array(
         'field'   => 'editor',
         'label'   => '*',
         'rules'   => 'required|min_length[5]'
      )
    );

    $this->form_validation->set_error_delimiters('<div class="text-error">', '</div>');
    $this->form_validation->set_rules($config);
    if ($this->form_validation->run() == FALSE)
    {
        $this->index();
    }
    else
    {
        $data = array(          
           'teknologi_title' => $this->input->post( "teknologi_title", TRUE ) ,
           'sub_title' => $this->input->post( "sub_title", TRUE ) ,
           'description' => html_entity_decode($this->input->post( "editor", TRUE )) ,
           'create_at' => date('Y-m-d H:i:s')
        );
        $this->load->model('Teknologi_m');
        $result=$this->Teknologi_m->insert($data);
        if($result!==FALSE)
        {
            $log_task = array(
                        'user_id' => $_SESSION['admin_user']['id'],
                        'user_email' => $_SESSION['admin_user']['email'],
                        'task' => json_encode($data),
                        'url_task' => $_SERVER['REQUEST_URI'],
                        'user_agent' => $_SERVER['HTTP_USER_AGENT'],
                        'ip_address' => $_SERVER['REMOTE_ADDR'],
                        'sdate' => date('Y-m-d H:i:s')
            );
            $this->master_lib->log_task($log_task);
                if($_FILES['userfile']['error'] != 4)
                {
                    $config['upload_path'] = './___userfiles/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['overwrite']=TRUE;
                    $config['file_type'] = 'image/jpeg';
                    $config['encrypt_name']=TRUE;
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    if($this->upload->do_upload())
                    {
                        $data=$this->upload->data();
                        $this->load->library("image_moo");
                        $this->image_moo->load($config['upload_path'].$data['file_name'])
                                        ->resize(160,160)
                                        ->save($config['upload_path'].$data['raw_name'].'_thumb'.$data['file_ext']);
                        $data = array(
                                'image_header' => $data['file_name'],
                                'image_thumb' => $data['raw_name'].'_thumb'.$data['file_ext']
                            );

                        $id = $this->Teknologi_m->get_id();
                        $this->Teknologi_m->update_photo($id,$data);
                    }

                }
                $this->session->set_flashdata('success', "Data has been saved");
                redirect('teknologi');
            }
            else
            {
                $this->session->set_flashdata('error', 'There is an error save data failed');
                redirect('teknologi/add');  
            }

        }
    }

Here's my model: 这是我的模型:

function update_photo($id=false,$data=false)
{
    $this->load->database();
    $this->db->where('id',$id);
    $this->db->update('teknologi',$data);
}

Try my sample code below should work. 试试下面的示例代码应该可以。 Codeigniter for some reason only handles one upload at a time, If you need to add multiple upload loads you could try using a for loop. 由于某种原因,Codeigniter一次只能处理一个上载。如果您需要添加多个上载,则可以尝试使用for循环。

But would recommend looking through here http://php.net/manual/en/features.file-upload.multiple.php 但建议您在这里浏览http://php.net/manual/zh/features.file-upload.multiple.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Upload extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
{
    $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload() {
    $files = $_FILES;
    $file_loop = count($_FILES['userfile']['name']);

    for($i=0; $i<$file_loop; $i++) {

        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

        $this->upload->initialize($this->file_config());

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        } else {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    } 
}

private function file_config() {   
    //  upload an image options
    $config = array();
    $config['upload_path'] = './upload/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;


    return $config;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM