Hello Guys, I have a Question, it possible storing image to dabase using blob in the Framework of CI? This is my Code in Controler.

function get_data_from_post(){
    $data['page_headline'] = $this->input->post('page_headline',TRUE);
    $data['page_title'] = $this->input->post('page_title',TRUE);
    $data['keywords'] = $this->input->post('keywords',TRUE);
    $data['description'] = $this->input->post('description',TRUE);
    $data['page_content'] = $this->input->post('page_content',TRUE);
    $imageName = $_FILES['featured_img']['name'];
    $data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['name']),TRUE);

    return $data;
}

but when i run i got this,
A PHP Error was encountered

Severity: Notice

Message: Undefined index: featured_img

Filename: controllers/article.php

Line Number: 44

A PHP Error was encountered

Severity: Notice

Message: Undefined index: featured_img

Filename: controllers/article.php

Line Number: 45

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(): Filename cannot be empty

Filename: controllers/article.php

Line Number: 45

what should i do? iam new in CI. Please help me. Thanks in Advance.

Recommended Answers

All 6 Replies

first you need to run the form validation library.
second check if the form_validation run is false.
third if false above, then call your model method to process the uploaded item.

doing this

$this->input->post('page_headline',TRUE);

will only run the data through the xss filter, but will not validate if it is empty or not.

example. Make sure to follow the php 5 syntax standard.

public function get_data_from_post(){

    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    ## anticipate any error or errors coming ahead of the form submission.
    $errors = array();

    ## layout your form validation rules
    $this->form_validation->set_rules('page_headline', 'Page Headline', 'required|trim|xss_clean|max_length[255]|alpha_numeric');

    ## add the remaining rules here

    ## check for any trapped errors

        if($this->form_validation->run() == FALSE){
            $errors['errors'] = $this->form_validation->set_err();

            ## if validation return true, send the user back to your form
            $this->load->view('upload_form',$errors);

        }

        else{
            ## call out the upload model
            $this->load->model('Article_model');

            if($this->Article_model->add_article()){

            ## it is a success, do whatever you need to do
                redirect('user/article_success','refresh');
        }

        }

 }

somewhere in your article model, you can do this. We can remove the xss parameter if we like. Just make sure you define it on your form validation rules.

public function add_article{

     $data['page_headline'] = $this->input->post('page_headline');
    $data['page_title'] = $this->input->post('page_title');
    $data['keywords'] = $this->input->post('keywords');
    $data['description'] = $this->input->post('description');
    $data['page_content'] = $this->input->post('page_content');
    $imageName = $_FILES['featured_img']['name'];
    $data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['name']));

    ## prepare the database query here


    return true;

    }

That's pretty much it. Just make sure to look closely on how the patterns are implemented. It takes practice in doing this.

Good luck to you..

i found errors on the code above

change this

 $data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['name']));

to this

 $data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['tmp_name']));

On your model, you still need to find out the true extession of the image. YOu may want to add another validation rules for it. That is only for the name of the file

Second option is to use the CodeIgniter Library as shown here.. use this in your controller..

Sir i am using HMVC instead of MVC in CI. this is my all function of controller to submit the data into the database. is this correct?

function get_data_from_db($update_id){
    $query = $this->get_where($update_id);
    foreach ($query->result() as $row) {
        $data['page_headline'] = $row->page_headline;
        $data['page_title'] = $row->page_title;
        $data['keywords'] = $row->keywords;
        $data['description'] = $row->description;
        $data['page_content'] = $row->page_content;
        $data['featured_img'] = $row->featured_img;
    }

    if(!isset($data)){
        $data="";
    }
    return $data;
}



function get_data_from_post(){
    $data['page_headline'] = $this->input->post('page_headline',TRUE);
    $data['page_title'] = $this->input->post('page_title',TRUE);
    $data['keywords'] = $this->input->post('keywords',TRUE);
    $data['description'] = $this->input->post('description',TRUE);
    $data['page_content'] = $this->input->post('page_content',TRUE);
    $imageName = $_FILES['featured_img']['name'];
    $data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['name']),TRUE);

    return $data;
}


function submit(){
        $this->load->library('form_validation');

        $this->form_validation->set_rules('page_headline', 'Page Headline', 'required|xss_clean');
        $this->form_validation->set_rules('page_content', 'Page Content', 'required|xss_clean');

        if ($this->form_validation->run($this) == FALSE){
            $this->addarticle();
        }else{

            $data = $this->get_data_from_post(); 
            $data['page_url'] = url_title($data['page_headline']);
            $update_id = $this->uri->segment(3);
            if(is_numeric($update_id)){
                $this->_update($update_id,$data);
            }else{
                $this->_insert($data);
            }

            redirect('/');
        }
}


function addarticle(){

    $update_id = $this->uri->segment(3);
    $submit = $this->input->post('submit',TRUE);

        if ($submit=="Submit") {
            $data = $this->get_data_from_post();
        } else {
            if(is_numeric($update_id)){
                $data = $this->get_data_from_db($update_id);
            }
        }

        if (!isset($data)) {
            $data = $this->get_data_from_post();
        }

    $data['update_id'] = $update_id; 



    $data['view_file'] = "addarticle";
    $this->load->module('template');
    $this->template->addarticle($data);
}

i dont know how to insert your code into my code. can you guide me to do this?

and this is my model function

function get_table() {
$table = "webpages";
return $table;
}


function _insert($data){
$table = $this->get_table();
$this->db->insert($table, $data);
}

thanks sir. but i try your code now. thank you sir..

Can you please tell me which HMVC modular extension are you currently using? There are few of them, but have minor difference in doing things.

Another thing is that this

$data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['name']),TRUE);

will not work. We need to try changing it to this

$data['featured_img'] = (file_get_contents($_FILES['featured_img']['name']),TRUE);

remember that is file... I did not even noticed.

On your database, you need to have an extension column and save the extension of your BLOB.

to get the extension of the BLOB, we can do it like this. There is another way of doing this effeciently, but this is the one that can work flawlessly on the BLOB.

@list(, , $image_type, ) = getimagesize($_FILES['featured_img']['tmp_name']);

if ($image_type == 3){
    $ext="png"; 
}elseif ($image_type == 2){
    $ext="jpeg";
}elseif ($image_type == 1){
    $ext="gif";
}

you can then compare the actual extension of the image to your extension allowed for upload.

The next step is to save the extension on ext column. The reason for this is for you to be able to show the BLOB image on your page. like this

$image_data=$row['image'];

header('Content-Length: '.strlen($image_data));
header("Content-type: image/".$row['ext']);
echo $image_data;

Okay, I must admit, I am not too lazy today. Here is the HMVC design diagram as it applies to Codeigniter functioning as HMVC framework. It is not like the Kohana or Fuel PHP, but the design pattern concept is there.

e872bbfa2623ccfdbec51d7e2e114e03

it should read "Application Modules are pretty much independent". Sorry about my wireless keyboard battery it think is dying. It cannot catch up on my typing speed :).

Unlike the conventional MVC pattern, the HMVC pattern in this particular framework isolates the modules, making them less interconnected. So the blog controller don't even know if there are other controllers. Because of this design patterns, we can create as many modules as our application demands expansion.

edit again..
I really need to change my batteries.. this is my third edit and it is missing a lot of letters and verbs here in there.

thanks for you help sir. its work properly now. im using this code.
$data['featured_img'] = (file_get_contents($_FILES['featured_img']['name']),TRUE);

instead of this
$data['featured_img'] = $this->input->post(file_get_contents($_FILES['featured_img']['name']),TRUE);
Problem Solved! ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.