0

如何在 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()

4

1 回答 1

0

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

do_upload($file);

所以换行:

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

对此:

if (!do_upload($file))
于 2013-11-08T13:54:06.793 回答