2013-02-13 2 views
0

아래 기능을 사용하여 내 이미지 파일을 내 서버 (로컬 호스트)에 업로드하고 있습니다. 괜찮아요 이미지가 업로드되고 있습니다. 하지만 두 개의 이미지 필드가 필요하므로 제출 버튼을 클릭하면 두 이미지를 모두 업로드해야합니다. 여기에 설명 된 기능을 사용했습니다. Codeigniter multiple file upload, 그러나 작동하지 않습니다.codeigniter에서 이미지를 업로드하는 중 사용자 파일이 필요합니까?

나는 오류입니다

A PHP Error was encountered 

Severity: Warning 

Message: is_uploaded_file() expects parameter 1 to be string, array given 

Filename: libraries/Upload.php 

Line Number: 161 

그럼 내가 이해할 수없는이 오류 메시지가 표시됩니다. 나는 하나의 이미지를 업로드하기 위해 사용하고있는 기능은 나는 또한 내가 내 선택의 입력 필드 이름을 변경할 수 있습니다 부탁 또한

function do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'jpeg|png|gif'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 

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

     if (! $this->upload->do_upload()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      //failed display the errors 
     } 
     else 
     { 
      //success 

     } 
    } 

입니다. 즉 나는 항상 <input type="file" name="userfile"/>을 구현해야합니다. 이름을 바꿀 수 있습니까? 변경하려고 시도하고 메시지가 나타납니다 파일을 선택하지 않았으므로 변경할 수 없습니다.

답변

5

제공된 링크에 표시된 것과 같이 업로드 된 파일을 반복해야합니다.

function do_upload() { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'jpeg|png|gif'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 

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

     foreach ($_FILES as $key => $value) { 

      if (!empty($value['tmp_name'])) { 

       if (! $this->upload->do_upload($key)) { 
        $error = array('error' => $this->upload->display_errors()); 
        //failed display the errors 
       } else { 
        //success 
       } 

      } 
     } 
} 

과 같은 HTML을 사용해보십시오 : 당신이 원하는대로

<input type="file" name="file1" id="file_1" /> 
<input type="file" name="file2" id="file_2" /> 
<input type="file" name="file3" id="file_3" /> 

당신은 입력 필드의 이름을 변경할 수 있습니다. 당신이 파일을 다중 선택 사용하는 경우

+0

질문에 게시 한 오류 메시지가 계속 나타납니다. – prakashchhetri

+0

실수가 수정되었습니다. –

+0

하나의 이미지 만 업로드하고 있습니다. – prakashchhetri

0

, 다음을 수행하십시오

HTML을 (HTML5 배열 이름 입력이 여러 개의 파일을 선택할 수 있도록 정보 파일) :

<input type="file" name="userfile[]"/> 

또는

<input type="file" name="userfile[]"/> 
<input type="file" name="userfile[]"/> 

PHP in CodeIgniter :

// load upload library (put your own settings there) 
$this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'],)); 

// normalise files array 
$input_name = 'userfile'; // change it when needed to match your html 
$field_names = array('name', 'type', 'tmp_name', 'error', 'size',); 
$keys = array(); 
foreach($field_names as $field_name){ 
    if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){ 
     $_FILES[$input_name.'_'.$key][$field_name] = $value; 
     $keys[$key] = $key; 
    } 
} 
unset($_FILES[$input_name]); // just in case 

foreach ($keys as $key){ 

    $new_file = $this->upload->do_upload($input_name.'_'.$key); 

    // do your stuff with each uploaded file here, delete for example: 
    $upload_data = $this->upload->data(); 
    unlink($upload_data['file_name']); 

} 
관련 문제