【问题标题】:Load and call helper in library [duplicate]在库中加载和调用助手[重复]
【发布时间】:2013-11-08 13:44:23
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter-2 helper


    【解决方案1】:

    如果您加载了帮助程序,您应该能够像这样调用该帮助程序中的函数:

    do_upload($file);
    

    所以换行:

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

    到这里:

    if (!do_upload($file))
    

    【讨论】:

    • 当我这样做时,我收到此错误消息:Call to undefined function do_upload()。我在构造函数中加载错误吗?
    • 你的form_helper中有do_upload函数吗?
    • @JohnSmith 如果将$this->ci->load->helper('form'); 移至函数upload_file 会发生什么?你的辅助功能是什么?助手必须是函数,而不是类。
    • 将其移入函数Call to undefined function do_upload() 时,我收到相同的错误消息。 do_upload() 是 CI 中辅助表单的原始部分。所以我自己没有用它做任何事情。这都是标准的。还有其他建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2015-05-20
    • 1970-01-01
    • 2011-12-16
    • 2012-01-08
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多