2017-09-05 4 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

function 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 경우 (C1 내지 $> upload-> do_upload의 ($ name_file)) { \t \t \t \t \t 은 (C1 내지 $> upload-> display_errors를 에코); 다른 \t \t \t \t} \t \t $ {Dder = $ C1-6 알킬> upload-> 데이터(); return $ Dder [ 'file_name']; } // 컨트롤러에 있음 – NewPhpProgramer101

답변

2

도우미 내부에서 라이브러리를 호출하고 확장하지 않는 것이 좋습니다. $this은 컨트롤러 및 모델에서만 작동하므로 get_instance()으로 먼저 전화해야합니다. 당신은 도우미 또는 라이브러리를 사용하고자하는 경우는 다음과 같이해야 얻을 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']; 
     } 
    } 
} 

당신이 설명이 필요한 경우 알려주세요. 이 질문에 대한 답변이 있으면 답변으로 표시하십시오. 감사!

관련 문제