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

时间:2015-01-18 07:14:58

标签: php codeigniter upload

我想使用codeigniter构建图库。但我不能用于多个图像上传。目前,我的代码仅用于单个图像上传并将其存储到数据库中。

这是我的控制器

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');  
            }

        }
    }

这是我的模特:

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

1 个答案:

答案 0 :(得分:0)

尝试下面的示例代码应该可行。 Codeigniter由于某种原因一次只处理一个上传,如果你需要添加多个上传加载,你可以尝试使用for循环。

但建议通过这里查看http://php.net/manual/en/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;
}
}