0

控制器

  <?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')

4

3 回答 3

1

您现在有一个带有键“upload_data”的数组 $data(您实际上不需要这样做 - 我希望他们会修复文档,但不要这样做)

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

从内存中,会有类似 $data['upload_data']['file_name']...

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

$this->uploadermodel->uploadcoupon(); 

需要通过 $data...

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

不要像你正在做的那样尝试从 $this->upload 中获取它

于 2013-02-03T23:06:13.183 回答
0

为此,您需要这个

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

//Now you can insert file name
$this->uploadermodel->uploadcoupon($data);
于 2013-02-04T02:51:32.277 回答
0

从文档:

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

因此,您需要指定要存储在数据库中的数组索引。更多信息请点击这里:

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

于 2013-02-04T07:24:52.337 回答