[英]Cannot use custom library in CodeIgniter 3
I want to use https://www.verot.net/php_class_upload_download.htm library in my project for resizing images.我想在我的项目中使用https://www.verot.net/php_class_upload_download.htm库来调整图像大小。 However, when I submit form it gives this error但是,当我提交表单时,它会出现此错误
I have loaded the library in the autoloader and I renamed library to "my_upload" and gave the same class name.我已经在自动加载器中加载了库,并将库重命名为“my_upload”并给出了相同的类名。
However, I do not know why this error occurs.但是,我不知道为什么会出现此错误。
And here is my controller:这是我的控制器:
<?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');
}
Where can be the problem?问题出在哪里?
imho the best way is to create a third party library for that copy your class into your folder application/third_party/upload/
恕我直言,最好的方法是创建一个第三方库,将您的类复制到您的文件夹application/third_party/upload/
and in your controller you simply inlcude this file like:在您的控制器中,您只需包含此文件,例如:
require_once(APPPATH."third_party/upload/my_upload.php");
$objUpload = new my_upload($_FILES);
If you really want a library for that try the following:如果您真的想要一个库,请尝试以下操作:
The problem is your library gets instantiated by CI - you don't really have control over the constructor问题是你的库被 CI 实例化了——你并没有真正控制构造函数
the only way you could do is to include a "wrapper" library eg您可以做的唯一方法是包含一个“包装器”库,例如
<?php
require_once(APPPATH."libraries/my_upload.php");
class Uploadwrapper_library
{
public function get($files)
{
return new my_upload($files);
}
}
in your controller you could do在你的控制器中你可以做
$this->load->library("uploadwrapper_library");
$objMyUpload = $this->uploadwrapper_library->get($_FILES);
Well if you look at the file - system/libraries/Upload.php it's constructor requires a parameter...好吧,如果您查看文件 - system/libraries/Upload.php它的构造函数需要一个参数...
public function __construct($config = array())
So with your MY_Upload class which is CI's way to allow you to override core classes, you need to follow suit with the class you are extending.因此,对于您的 MY_Upload 类,这是 CI 允许您覆盖核心类的方式,您需要效仿您正在扩展的类。
You've not shown your constructor so I don't know what you have attempted with your constructor...你没有展示你的构造函数,所以我不知道你用构造函数尝试了什么......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.