在库中加载和调用帮助器

时间:2013-11-08 13:44:23

标签: php codeigniter-2 helper

如何在Codeigniter中调用库中的助手?

我的代码:

class Upload
{
    protected $ci;  

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

        // Load helper
        $this->ci->load->helper('form');
    }


    function upload_file($file, $file_types = 'gif|jpg|png', $max_size = 1000, $max_width = 200, $max_height = 300)
    {
        // Set params
        $config['upload_path'] = 'attached-files';
        $config['allowed_types'] = $file_types;
        $config['max_size'] = $max_size;

        // Check if max_width is set equals to it is a image
        if(isset($max_width))
        {
            $config['max_width']    = $max_width;
            $config['max_height']   = $max_height;  
        }

        // Try to upload the file
        if (!$this->ci->upload->do_upload($file))
        {
            $data['profile_image_upload'] = array('error' => $this->ci->upload->display_errors());
        }
        else
        {
            $data['upload_data'] = $this->ci->upload->data();
        }

        return $data;
    }

我正在尝试构建一个通用库,我想在上传文件时调用它,如果它是pdf,图像或其他文件则是独立的。

但我在以下代码中收到错误:

if (!$this->ci->upload->do_upload($file))

错误讯息: 调用未定义的方法Upload :: do_upload()

1 个答案:

答案 0 :(得分:0)

如果你加载了一个助手,你应该可以像这样调用那个助手中的函数:

do_upload($file);

所以换行:

if (!$this->ci->upload->do_upload($file))

对此:

if (!do_upload($file))