Codeigniter HMVC - 无法在函数中加载库

时间:2017-09-13 18:27:18

标签: php codeigniter load hmvc

我遇到了Codeigniter HMVC这个问题。

当我想加载诸如'upload'或。之类的库时 'image_lib',如果我将它加载到类的本地函数中,则该实例始终为null。但是,当我在构造函数中实例化一个对象时,加载成功,我可以调用库类的函数。

以下是代码:

<?php
class Listed_items extends MX_Controller {

function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('upload'); // this works
$this->load->library('image_lib'); // this works
$this->form_validation->set_ci($this);
}

function do_upload($item_url) {
$submit = $this->input->post('submit', true);
if ($submit == "cancel") {
  redirect('listed_items/create_item/'.$item_url);
} else if ($submit == "upload") {
  $config['upload_path'] = './big_pics/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = 300;
  $config['max_width'] = 3036;
  $config['max_height'] = 1902;
  $file_name = $this->site_security->generate_random_string(16);
  $config['file_name'] = $file_name;

  $this->load->library('upload', $config); // this calls on null
  }
 }
}

如果有人可以帮助解决这个问题,那就太棒了。

1 个答案:

答案 0 :(得分:0)

您可以针对您的问题尝试此解决方案。

  

请更改您的控制器文件。

<?php
class Listed_items extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->form_validation->set_ci($this);
    }
    function do_upload($item_url) {
        $submit = $this->input->post('submit', true);
        if ($submit == "cancel") {
            redirect('listed_items/create_item/' . $item_url);
        } else if ($submit == "upload") {
            $config['upload_path'] = './big_pics/';
            $config['allowed_types'] = config_item('allowed_files');
            $config['max_size'] = '20240000245';
            $config['overwrite'] = TRUE;
            $config['encrypt_name'] = TRUE;
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $imgage_width=!empty($width) ? $width :60;
            $imgage_height=!empty($height) ? $height :60;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload($field)) {
                $error = $this->upload->display_errors();
                $type = "error";
                $message = $error;
                set_message($type, $message);
                return FALSE;
                // uploading failed. $error will holds the errors.
            } else {
                $fdata = $this->upload->data();
                $configer =  array(
                  'image_library'   => 'gd2',
                  'source_image'    =>  $fdata['full_path'],
                  'maintain_ratio'  =>  TRUE,
                  'width'           =>  $imgage_width,
                  'height'          =>  $imgage_height,
                );
                $this->load->library('image_lib');
                $this->image_lib->clear();
                $this->image_lib->initialize($configer);
                $this->image_lib->resize();
                $img_data ['path'] = $config['upload_path'] . $fdata['file_name'];
                return $img_data;
                // uploading successfull, now do your further actions
            }
        }
    }
}
?>

我希望这会对你有所帮助。