【问题标题】:can a custom helper extend library in codeigniter自定义助手可以在 codeigniter 中扩展库吗
【发布时间】:2018-02-14 01:13:51
【问题描述】:

我正在尝试为图像上传做一个助手。我可以在自定义助手中扩展库吗?我曾尝试使用它,但它始终无法使用。

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

}

【问题讨论】:

  • 你能发布你如何扩展它,你遇到什么错误?谢谢
  • 函数 FileUploadd($name_file) { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $ci =& get_instance(); $ci->load->library('upload', $config); if ( $ci->upload->do_upload($name_file)) { echo $ci->upload->display_errors(); } else { $Dder=$ci->upload->data();返回 $Dder['file_name']; } //在控制器中

标签: codeigniter helper extend


【解决方案1】:

我看到你想在你的助手中调用库而不是扩展它。您应该首先调用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'];
        }
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多