0

controller

  <?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();               
                }
            }
    }
    ?>

model

   <?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); 
        }

    }   
    ?>

I'm trying to gather the value of $config['file_name'] and send it with my model. How do I do so? Currently it's trying to upload: VALUES (Array, '2013-02-03 20:20:01')

3 Answers 3

1

You now have an array $data with a key 'upload_data' (you don't actually need to do it this way - I wish they would fix the docs, but leave it)

So, do a var_dump($data['upload_data']) and you will see all the information about the file you just uploaded.

Going from memory, there will be something like $data['upload_data']['file_name']...

So, just pass that value plus your full insert data to your model.

$this->uploadermodel->uploadcoupon(); 

Need to have $data passed...

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

don't try to grab it from $this->upload like you are doing

Sign up to request clarification or add additional context in comments.

Comments

0

For this you need this

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

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

Comments

0

From the docs:

$this->upload->data() is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype.

Therefore you need to specify an array index that you want to store in database. More information is available here:

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

2 Comments

you are correct, but I think that is bad design to assume inside your model function that an upload was made. The model function should handle the insert only, and be passed the validated data to insert. I should have said that more clearly in my answer
yes you are right... My answer is focusing on error that he got while inserting data.. I do not focus on design patterns and other things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.