Kārlis Janisels
Kārlis Janisels

Reputation: 1295

Problems storing image url in database on upload (CodeIgniter)

This is widely discussed topic:how to save image url in database on upload?

I have read them but still can not understand why my controller is not working. I followed CodeIgniters documentation on uploading files and created controler that uploads file in desired directory, so far so good. I now want to save image url in database on upload. I modified code from this question but I have multiple errors that I can not get around.

1.

Message: Illegal string offset 'file_name'

Filename: controllers/Upload.php

Line Number: 36

2.Message: Undefined property: Upload::$db

Filename: controllers/Upload.php

Line Number: 37

3.Message: Call to a member function insert() on null

Filename: controllers/Upload.php

Line Number: 37

Here is my controller(I have test table puzz with two fields - auto increment primary key id and image_url):

<?php

class Upload extends CI_Controller {

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

        public function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

        public 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;

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

                if ( ! $this->upload->do_upload('userfile'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('upload_form', $error);
                }
                else
                {
                        $data = $this->upload->data();
                        $file_array = $this->upload->data('file_name');
                        $image['image_url'] = $file_array['file_name'];
                        $this->db->insert('puzz', $image);
                        $this->load->view('upload_success', $data);
                }
        }
}
?>

Will appreciate if someone could point to wrongs in my code!

Upvotes: 0

Views: 691

Answers (1)

Rakesh Sojitra
Rakesh Sojitra

Reputation: 3658

add this before using insert

$this->load->database();

OR change constructor

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

OR change in autoload.php

$autoload['libraries'] = array('database');

AND change this code block to

$data = $this->upload->data();
$image['image_url'] = $data['file_name'];
$this->db->insert('puzz', $image);
$this->load->view('upload_success', $data);

Upvotes: 1

Related Questions