2012-09-06 3 views
0

내가 작업하는 양식에는 5 명이 들어 있습니다. 선택적으로, 사용자는 5 명 각각에 대해 사진을 업로드 할 수 있습니다. 이미지가 업로드되지 않은 결과 배열 $에 기본 이미지를 삽입하려고합니다. 도움말 크게 감사하겠습니다.CodeIgniter 이미지 업로드 (배열에 빈 삽입)

function multiple_upload($upload_dir = APPPATH, $config = array()) 
{ 
    $CI =& get_instance(); 
    $files = array(); 

    if(empty($config)) 
    { 
     $config['upload_path'] = realpath($upload_dir . '/uploads'); 
     $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png'; 
     $config['max_size']  = '2048'; 
    } 

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

    $errors = FALSE; 
    $this->load->library('image_lib'); // moved outside loop so it'll work 
    foreach($_FILES as $key => $value) 
    { 
     if(! empty($value['name'])) 
     { 
      if(! $CI->upload->do_upload($key)) 
      { 
       $data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file 
       $CI->load->vars($data); 

       $errors = TRUE; 
      } 
      else 
      { 

       // resize the saved image 
       $path = $CI->upload->data('full_path'); 
       //echo $path['full_path'] . "<Br/>"; 
       $config = array(
        'source_image' => $path['full_path'], 
        'new_image' => $upload_dir . 'uploads/thumbs', 
        'maintain_ration' => true, 
        'width' => 150, 
        'height' => 150 
       ); 

       $this->image_lib->initialize($config); 
       $this->image_lib->resize(); 
       // Build a file array from all uploaded files 
       $files[] = $CI->upload->data(); 


      } 
     } 
    } 

    // There was errors, we have to delete the uploaded files 
    if($errors) 
    { 
     foreach($files as $key => $file) 
     { 
      @unlink($file['full_path']); 
     } 
    } 
    elseif(empty($files) AND empty($data['upload_message'])) 
    { 
     $CI->lang->load('upload'); 
     $data['upload_message'] = ERR_OPEN.$CI->lang->line('upload_no_file_selected').ERR_CLOSE; 
     $CI->load->vars($data); 
    } 
    else 
    { 

     return $files; 

    } 

} 

답변

0

업로드 한 파일의 유효성을 검사 한 후 확인하는 것이 더 쉽습니다.

예 : 각 그림마다 하나씩 다섯 개의 빈 요소가있는 배열을 초기화 한 다음 업로드 된 각 파일의 파일 이름을 적절한 배열 요소로 설정합니다. 작업이 끝나면 비어있는 모든 요소를 ​​실행하고 파일 (또는 파일 경로)을 기본 이미지로 압정하기 만하면됩니다.

이렇게하면 파일 업로드 및 유효성 검사를 가능한 한 바닐라 상태로 유지하고 페이지의 비즈니스 논리를 개별적으로 처리 할 수 ​​있습니다.

관련 문제