将数据从控制器传递到模型Codeigniter

时间:2013-02-03 20:21:08

标签: codeigniter file-upload models

控制器

  <?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('uploaderview', array('error' => ' ' ));
            }

            function do_upload(){
                $config['upload_path'] = './upl0d/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2048'; //2mb
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $config['encrypt_name'] = FALSE;
                $config['overwrite'] = FALSE;

                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload()){
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('uploaderview', $error);
                }
                else{
                    ## Insert into filesystem.
                    $data = array('upload_data' => $this->upload->data());
                    ## load the success page.
                    $this->load->view('uploadsuccess', $data);
                    ## Insert into db
                    ## then insert the img name into the database
                    $this->load->model('uploadermodel');
                    $this->uploadermodel->uploadcoupon();               
                }
            }
    }
    ?>

模型

   <?php

    class Uploadermodel extends CI_Model{

        function __construct(){
            // Call the Model constructor
            parent::__construct();
        }

        function uploadcoupon($data){
            $uploadFileName = $this->upload->data();
            $currentDt = date('Y-m-d H:i:s');
            $data = array('fileNameUploaded'=>$uploadFileName,'date'=>$currentDt);
            $this->db->insert('Coupon', $data); 
        }

    }   
    ?>

我正在尝试收集$config['file_name']的值并将其与我的模型一起发送。我该怎么办?目前正在尝试上传:VALUES (Array, '2013-02-03 20:20:01')

3 个答案:

答案 0 :(得分:1)

您现在拥有一个数组$ data,其中包含一个键&#39; upload_data&#39; (你实际上并不需要这样做 - 我希望他们能修复这些文档,但保留它们)

所以,做一个var_dump($ data [&#39; upload_data&#39;]),你会看到有关你刚刚上传的文件的所有信息。

从内存开始,会有类似$ data [&#39; upload_data&#39;] [&#39; file_name&#39;] ......

因此,只需将该值加上完整的插入数据传递给模型即可。

$this->uploadermodel->uploadcoupon(); 

需要传递$ data ...

$this->uploadermodel->uploadcoupon($data);

请勿尝试从$ this-&gt;上传中抓取它,就像你正在做的那样

答案 1 :(得分:0)

为此你需要这个

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

//Now you can insert file name
$this->uploadermodel->uploadcoupon($data);

答案 2 :(得分:0)

来自文档:

  

$ this-&gt; upload-&gt; data()是一个辅助函数,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是   数组原型。

因此,您需要指定要存储在数据库中的数组索引。有关更多信息,请访问:

http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html