Codeigniter多个图像上传并存储到数据库 [英] Codeigniter Multiple image upload and store to database

查看:50
本文介绍了Codeigniter多个图像上传并存储到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Codeigniter构建图片库.但是我不能用于多张图片上传.目前,我的代码仅适用于单张图片上传并将其存储到数据库中.

i'm want to build image gallery using 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.

这是我的控制器

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

推荐答案

请尝试下面的示例代码.由于某种原因,Codeigniter一次只能处理一个上载.如果您需要添加多个上载,则可以尝试使用for循环.

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.

但建议您在此处浏览 http://php.net/手册/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;
}
}

这篇关于Codeigniter多个图像上传并存储到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆