无法在CodeIgniter 3中使用自定义库

时间:2017-10-13 10:03:16

标签: php codeigniter

我想在我的项目中使用https://www.verot.net/php_class_upload_download.htm库来调整图像大小。但是,当我提交表单时,会出现此错误

enter image description here

我已经在自动加载器中加载了库,并将库重命名为“my_upload”并给出了相同的类名。

enter image description here

但是,我不知道为什么会出现这种错误。

这是我的控制器:

<?php
    class Blog extends CI_Controller{

        function __construct() {
            parent::__construct();
        }


        public function add() {

            if(!$this->session->userdata('logged_in')) {
                $this->session->set_flashdata('not_loggedin','<div class="alert alert-success text-center">Please Login</div>');
                redirect('login');
             }
            $data['title'] = 'Ədd nyus';


            $data['author'] = $this->Blog_model->get_author();
            $data['category'] = $this->Blog_model->get_category();

            $this->load->view('templates/header');
            $this->load->view('blog/add', $data);
            $this->load->view('templates/footer');
        }

        public function create() {

            if(!$this->session->userdata('logged_in')) {
                $this->session->set_flashdata('not_loggedin','<div class="alert alert-success text-center">Please Login</div>');
                redirect('login');
             }

            //insert image    

             $now = date("YmdHis");
             $this->my_upload->upload($_FILES["userfile"]);
             if ( $this->my_upload->uploaded == true  ) {
              $this->my_upload->allowed               = array('jpg|png');
              $this->my_upload->file_new_name_body    = 'image_resized' . $now;
              $this->my_upload->image_resize          = true;
              $this->my_upload->image_ratio_fill      = true;
              $this->my_upload->image_x               = 360;
              $this->my_upload->image_y               = 236;
              // $this->my_upload->image_ratio_y         = true;
              $this->my_upload->process('C:\xampp\htdocs\edu-center\assets\img\blog');
              if ( $this->my_upload->processed == true ) {
                $this->my_upload->clean();
                $post_image = $_FILES["userfile"]["name"];  
              }
            } else {
                $post_image = '';
            }

            //insert the user registration details into database
            $randnum = mt_rand(100000,999999);
            $slugtitle = mb_strtolower($this->input->post('title_az'), 'UTF-8') . '-' .$randnum;
            $slug = url_title($slugtitle);

            $post_image = str_replace(' ', '_', $post_image);   
            $post_image = preg_replace('/_+/', '_', $post_image);

            date_default_timezone_set('Asia/Baku');
            $data = array(
                'title_az' => strip_tags($this->input->post('title_az')),
                'title_rus' => strip_tags($this->input->post('title_rus')),
                'author_id' => $this->input->post('author_id'),
                'category_id' => strip_tags($this->input->post('category')),
                'body_az' => $this->input->post('body_az'),
                'body_rus' => $this->input->post('body_rus'),
                'date' => date("d-m-Y"),
                'news_slug' => $slug,
                'img' => $post_image
            );

        $this->Blog_model->add_news($data);

        $this->session->set_flashdata('changed_msg','<div class="alert alert-success text-center">Ваши изменения были сохранены!</div>');
        redirect('blog');
    }

问题出在哪里?

2 个答案:

答案 0 :(得分:1)

imho最好的方法是为此创建第三方库 将您的班级复制到文件夹application/third_party/upload/

在您的控制器中,您只需将此文件包括在内:

require_once(APPPATH."third_party/upload/my_upload.php");
$objUpload = new my_upload($_FILES);

如果您真的想要一个库,请尝试以下方法:

问题是您的库被CI实例化 - 您实际上无法控制构造函数

你唯一能做的就是包含一个“包装”库 e.g。

<?php

require_once(APPPATH."libraries/my_upload.php");

class Uploadwrapper_library
{
    public function get($files)
    {
        return new my_upload($files);
    }
}
你可以在你的控制器中

$this->load->library("uploadwrapper_library");
$objMyUpload = $this->uploadwrapper_library->get($_FILES);

答案 1 :(得分:0)

好吧,如果你查看文件 - system / libraries / Upload.php ,它的构造函数需要一个参数......

public function __construct($config = array())

因此,使用您的MY_Upload类是CI允许您覆盖核心类的方法,您需要跟随您正在扩展的类。

您尚未展示构造函数,因此我不知道您对构造函数的尝试...