问题
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.
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);
}
回答1:
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.
But would recommend looking through here 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;
}
}
来源:https://stackoverflow.com/questions/28007970/codeigniter-multiple-image-upload-and-store-to-database