尝试在codeigniter中的一列中上传两个图像file_path

时间:2014-10-15 04:55:22

标签: php mysql codeigniter

我正在尝试在一列中上传两个图像file_path。在我的模型中,我从$ filepath成功获得第一个图像路径,但$ a无法正常工作我也感到困惑,$ a正在获取我的第二个图像路径

请帮助我

提前致谢。

我的控制器

<?php

class upload extends CI_Controller {

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

    function index()
    {
        $this->load->view("main_temp/header.php");
        $this->load->view('post_ad_views', array('error' => ' ' ));
        $this->load->view("main_temp/footer.php");
    }

// controller
function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['file_name'] = $new_file_name;
    $this->load->library('upload', $config);


    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view("main_temp/header.php");
        $this->load->view('post_ad_views', $error);
        $this->load->view("main_temp/footer.php");
    }
    else
    {
        // success
        $data = array('upload_data' => $this->upload->data());

        // a model that deals with your image data (you have to create this)
        $this->load->model('submit_ad_model');
        $this->submit_ad_model->ad_post();

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


   }


}
}
?>

我的模特

<?php
class submit_ad_model extends CI_Model
{

    function ad_post()
    { 

        $filepath = $this->upload->data()['file_name']; 

         $a = $this->upload->data()['file_name']; 

        $this->db->query("insert into ads (ad_pic)  values ('$filepath,$a')");
?>

我的观点

<input type="file" name="userfile" class="upload image"  id="userfile" />
<input type="file" name="userfile2" class="upload image"  id="userfile" />

1 个答案:

答案 0 :(得分:0)

您不想获得第二个上传项目,您需要使用循环:

       $upload_data = array();
       foreach($_FILES as $key => $value){
           $this->upload->do_upload($key);
           $upload_data[$key] = $this->upload->data($key);
        }
         $this->submit_ad_model->ad_post($upload_data);