NewPhpProgramer101
NewPhpProgramer101

Reputation: 19

can a custom helper extend library in codeigniter

I am trying to make a helper for Image Uploading. Can I extend the library inside a custom helper? I had try to use it and it never works.

<?php  
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function FileUploadd($r)
{

    $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($r))
        {
            echo  $this->upload->display_errors();

        }
        else
        {
                    $Dder=$this->upload->data();
                    return $Dder['file_name'];
                 }

}

Upvotes: 1

Views: 476

Answers (1)

chad
chad

Reputation: 838

I see you want to call the library inside your helper and not to extend it. You should call get_instance() first because $this only works oncontrollers and models. If you want to use a helper or library you need to call the get Codeigniter's instance first

Your code should look like this:

class Your_helper_name{

    private $CI;

    function __construct(){
        $this->CI =& get_instance();
    }

    function FileUploadd($r) {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

        if ( ! $this->CI->upload->do_upload($r))
        {
            echo  $this->CI->upload->display_errors();

        }
        else
        {
            $Dder=$this->CI->upload->data();
            return $Dder['file_name'];
        }
    }
}

Let me know if you need clarification. If this answers your question, please mark this as answer. Thanks!

Upvotes: 3

Related Questions