2017-09-05 127 views
1

我想为图像上传做一个帮手。我可以在自定义帮助器中扩展库吗?我试图使用它,它从来没有工作。可以在codeigniter中自定义帮助程序扩展库

<?php 
/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

function FileUploadd($r) 
{ 

    $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

     $this->load->library('upload', $config); 

     if (! $this->upload->do_upload($r)) 
     { 
      echo $this->upload->display_errors(); 

     } 
     else 
     { 
        $Dder=$this->upload->data(); 
        return $Dder['file_name']; 
       } 

} 
+0

你可以发表你如何扩展它,你遇到什么错误?谢谢 – chad

+0

函数FileUploadd($ name_file) { $ config ['upload_path'] ='./uploads/'; \t \t $ config ['allowed_types'] ='gif | jpg | png'; \t \t $ config ['max_size'] \t ='100'; \t \t $ config ['max_width'] ='1024'; \t \t $ config ['max_height'] ='768'; $ ci =&get_instance(); \t \t $ ci-> load-> library('upload',$ config); \t \t如果($ CI-> upload-> do_upload($ name_file)) \t \t { \t \t \t回声$ CI-> upload->的display_errors(); \t \t} \t \t别的 \t \t { $ Dder = $ CI-> upload->数据(); return $ Dder ['file_name']; } //在控制器中 – NewPhpProgramer101

回答

2

我看到你想调用你的帮手内的库,而不是扩展它。您应该先致电get_instance(),因为$this仅适用于控制器和型号。如果你想使用一个辅助或库,你需要调用获得CodeIgniter的情况下第一

您的代码应该是这样的:

class Your_helper_name{ 

    private $CI; 

    function __construct(){ 
     $this->CI =& get_instance(); 
    } 

    function FileUploadd($r) { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

     $this->CI->load->library('upload', $config); 

     if (! $this->CI->upload->do_upload($r)) 
     { 
      echo $this->CI->upload->display_errors(); 

     } 
     else 
     { 
      $Dder=$this->CI->upload->data(); 
      return $Dder['file_name']; 
     } 
    } 
} 

让我知道你是否需要澄清。如果这回答你的问题,请将其标记为答案。谢谢!