可以在codeigniter中使用自定义帮助程序扩展库

时间:2017-09-05 17:04:09

标签: codeigniter helper extend

我正在尝试为Image Uploading做一个帮手。我可以在自定义助手中扩展库吗?我曾尝试使用它,它永远不会起作用。

<?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'];
                 }

}

1 个答案:

答案 0 :(得分:3)

我看到你想在帮助器内调用库而不是扩展它。您应首先致电get_instance(),因为$this仅适用于控制器和型号。如果你想使用帮助器或库,你需要先调用get Codeigniter的实例

您的代码应如下所示:

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'];
        }
    }
}

如果您需要澄清,请告诉我。如果这回答了您的问题,请将此标记为答案。谢谢!