2017-10-13 58 views
0

我想在我的項目中使用https://www.verot.net/php_class_upload_download.htm庫來調整圖像大小。然而,當我提交的形式,它給這個錯誤無法在CodeIgniter 3中使用自定義庫

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'); 
    } 

哪裏是什麼問題?

+0

您可能忽略'()''$這個 - > my_upload->上傳()'和'$ this-> my_upload-> processed()' – user4419336

+0

@ wolfgang1983同樣的錯誤:( –

+0

只是指出你已經加倍了'$ this-> my_upload-> uploaded'這 – user4419336

回答

1

恕我直言,最好的方法是創建一個第三方庫爲 複製你的類到該文件夾​​application/third_party/upload/

,並在你的控制器,你只需inlcude此文件,如:

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

如果你真的想要一個庫試試以下內容:

問題是你的庫被CI實例化 - 你沒有真正控制構造函數

您可以做的唯一方法是包含一個「包裝」庫

<?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); 
+0

這意味着如果我使用第一個你應該使用'$ objUpload->'而不是'$ this-> my_upload->'? –

+0

是的 - 那就是正確的 – sintakonte

+0

它會將數據插入到數據庫中,但是除了img名稱之外,它會拋出這些錯誤。 ://imgur.com/a/x7G6I –

0

那麼,如果你看一下這個文件 - 系統/庫/ upload.php的它的構造函數需要一個參數...

public function __construct($config = array()) 

所以你的MY_Upload類是CI的方式,可以讓你覆蓋核心類,你需要按照你正在擴展的類來進行。

你無法看到您的構造函數,所以我不知道你已經嘗試用你的構造是什麼?

+0

我沒有在my_upload.php構造函數 –

+0

這可能有幫助... https://stackoverflow.com/questions/24070590/codeigniter-file-upload-with-verot -upload-類和懸浮窗-JS – TimBrownlaw

相關問題