【问题标题】:Uploading multiple images with codeigniter to a database使用 codeigniter 将多个图像上传到数据库
【发布时间】:2015-12-20 22:29:31
【问题描述】:

提交表单时,我的函数一次只上传一张图片。我不能一次上传多张图片。这是一个大问题,因为我正在建立一个汽车销售网站,人们需要上传多张汽车图片。

我的 upload.php 控制器:

<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');

}
function index()
{
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('upload_form', array('error' => ' ' ));

}

  function do_upload()
{
if($this->input->post('upload'))
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '1024';
$config['max_width']  = '1024';
$config['max_height']  = '768';
 $this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{

$data=$this->upload->data();
$this->thumb($data);
$file=array(

'img_name'=>$data['raw_name'],
'thumb_name'=>$data['raw_name'].'_thumb',
'ext'=>$data['file_ext'],
'upload_date'=>time()
);
$this->upload_model->add_image($file);
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
} 
else
{
 redirect(site_url('upload'));
}
}   

 function thumb($data)
{
$config['image_library'] = 'gd2';
$config['source_image'] =$data['full_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 160;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}

我的upload_form.php 视图:

   <?php $attributes = array('name' => 'myform');
 echo form_open_multipart('/upload/do_upload',$attributes);?>

  <input type="file" name="userfile" size="20" />

  <input type="submit" value="upload" name="upload" />

  <?php echo form_close(); ?>

我的 upload_model.php 模型:

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Upload_model extends CI_Model {
  public function __construct()
 {
 parent::__construct();
 }
 function add_image($data)
 {

 $this->db->insert('jobs',$data);
 }
 }

我无法以允许多张图片上传的方式修改该功能。我将非常感谢任何形式的指导或帮助。提前谢谢!

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:
       #####################
        # Uploading multiple# 
        #     Images        #
        #####################
    
    
        $files = $_FILES;
        $count = count($_FILES['uploadfile']['name']);
        for($i=0; $i<$count; $i++)
                {
                $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
                $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
                $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
                $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
                $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());//function defination below
                $this->upload->do_upload('uploadfile');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
    
                }
              $fileName = $images;
    

    代码中发生了什么?

    $_FILE---->它是通过 POST 方法上传到当前脚本的项目的关联数组。进一步查看LINK

    它是一个可在所有脚本范围内使用的自动变量

    function set_upload_options()
      { 
      // upload an image options
             $config = array();
             $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
             $config['remove_spaces']=TRUE;
             $config['encrypt_name'] = TRUE; // for encrypting the name
             $config['allowed_types'] = 'gif|jpg|png';
             $config['max_size'] = '78000';
             $config['overwrite'] = FALSE;
             return $config;
      }
    

    在你的 html 标记中不要忘记:

    Input name must be be defined as an array i.e. name="file[]"
    
    Input element must have multiple="multiple" or just multiple
    
    3.$this->load->library('upload'); //to load library
    
    4.The callback, $this->upload->do_upload() will upload the file selected in the given field name to the destination folder.
    
    5.And the callback $this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-18
      • 2018-06-26
      • 1970-01-01
      • 2023-04-03
      • 2015-01-01
      • 2016-09-05
      • 2018-05-09
      • 2011-08-14
      相关资源
      最近更新 更多