2013-03-16 5 views
1

예를 들어 foo.png 파일을 업로드하는 경우 업로드 컨트롤러에서 "foo.png"문자열을 가져 오는 방법은 무엇입니까?Codeignator 업로드 컨트롤러에서 이미지 이름을 얻으려면 어떻게해야합니까?

컨트롤러 코드는 다음과 같습니다

<?php 

class Upload extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
     $this->load->database(); 
    } 

    function do_upload($folder) 
    { 
     $config['upload_path'] = './userdata/'. $folder . '/'; 
     $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()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      echo $this->upload->display_errors(); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 
      echo "<p>File sucesfully uploaded</p>"; 

      $filename = // How do I get the filename here 

     } 
    } 
} 
?> 

는 어떻게 업로드 된 파일의 파일 이름에 $filename을 설정할 수 있습니까?

답변

1
echo $data['raw_name'].$data['file_ext']; 

트릭을해야

예를 들어, 당신은 official CI manual에서 이미지

if($this->upload->do_upload('upload_data')) { 
$data = $this->upload->data(); 
echo $data['raw_name'].$data['file_ext']; 
} 
4

업로드 : 귀하의 경우 그래서

$this->upload->data() 
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype: 
Array 
(
    [file_name] => mypic.jpg 
    [file_type] => image/jpeg 
    [file_path] => /path/to/your/upload/ 
    [full_path] => /path/to/your/upload/jpg.jpg 
    [raw_name]  => mypic 
    [orig_name] => mypic.jpg 
    [client_name] => mypic.jpg 
    [file_ext]  => .jpg 
    [file_size] => 22.2 
    [is_image]  => 1 
    [image_width] => 800 
    [image_height] => 600 
    [image_type] => jpeg 
    [image_size_str] => width="800" height="200" 
) 

당신은 당신이 가지고있는 파일에 대한 필요한 모든 정보를 포함해야 $this->upload->data() 함수의 결과를 보유하고있는 $data 변수를 업로드되었습니다.

그리고 특히 $data['upload_data']['file_name']이 당신이 찾고있는 것입니다.

2

시험해보세요.

$data = $this->upload->data(); 
echo $data['file_name']; 
관련 문제