2016-09-08 3 views
1

CodeIgniter 3에서 이미지를 여러 번 업로드 할 때 문제가 있습니다. MY_Upload Class을 시도했지만 다른 코드를 사용해 보았지만 나에게 맞는 것은 없습니다.다중 업로드 오류

컨트롤러

function do_uploadMultiple(){ 
     $config['upload_path'] = './assets/page/img/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 
     $config['encrypt_name'] = TRUE; 

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

     $nameField = 'imagem'; 
     if (! $this->upload->do_upload($nameField)){ 
      $error = array('error' => $this->upload->display_errors()); 
     }else{ 
      $foto = array('upload_data' => $this->upload->data()); 

      return $foto['upload_data']['file_name']; 

     } 
    } 

    public function cadastrarPage(){ 

     $data['nome_menu'] = $this->input->post('nome'); 
     $data['titulo'] = $this->input->post('titulo'); 
     $data['text'] = $this->input->post('text'); 
     $data['video'] = $this->input->post('video'); 
     $teste['imagem'] = $this->input->post('imagem'); 

     $id_abrase = $this->Abrase_model->cadastrarPage($data); 

     for($i = 0; $i < count($_FILES['imagem']['name']); $i++){ 
      $img['id_abrase'] = $id_abrase; 
      $img['imagem'] = $this->do_uploadMultiple(); 

      $result = $this->Abrase_model->gravarImagem($img); 
      if($result){ 
       echo 'yeah'; 
      }else{ 
       echo 'fail'; 
      } 
     } 


    } 

보기

<form method="post" action="<?php echo base_url('index.php/abrase/cadastrarPage'); ?>" enctype="multipart/form-data"> 
       <div class="form-group"> 
        <label for="nome">Nome da página no menu</label> 
        <input id="nome" name="nome" type="text" class="form-control"> 
       </div> 
       <div class="form-group"> 
        <label for="titulo">Título</label> 
        <input id="titulo" name="titulo" type="text" class="form-control"> 
       </div> 
       <div class="form-group"> 
        <label for="text">Texto da Página</label> 
        <textarea class="form-control" name="text" id="text" cols="30" rows="10"></textarea> 
       </div> 
       <div class="form-group"> 
        <label for="imagem">Selecione as imagens </label> 
        <input class="file" name="imagem[]" id="imagem" type="file" multiple data-show-upload="false"> 
       </div> 
       <div class="form-group"> 
        <label for="video">Vídeo</label> 
        <input type="text" name="video" id="video" class="form-control"> 
       </div> 
       <div class="form-group"> 
        <label for="pdf">Arquivos</label> 
       </div> 
       <div class="form-group"> 
        <input class="btn btn-info" type="submit" value="Cadastrar"> 
       </div> 
      </form> 

모델

public function cadastrarPage($data){ 
     $this->db->set('data_add', 'NOW()', FALSE); 
     $this->db->set('excluido', 0); 

     $this->db->insert('abrase', $data); 
     return $this->db->insert_id(); 
    } 

public function gravarImagem($data){ 
     return $this->db->insert('abrase_img', $data); 
    } 

이제 오류

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: 412 

Backtrace: 

File: C:\xampp\htdocs\Tattica\ABRASE\application\controllers\Abrase.php 
Line: 32 
Function: do_upload 

File: C:\xampp\htdocs\Tattica\ABRASE\application\controllers\Abrase.php 
Line: 54 
Function: do_uploadMultiple 

File: C:\xampp\htdocs\Tattica\ABRASE\index.php 
Line: 315 
Function: require_once 

나는 웹에서 찾은 모든 것을 시도했지만 그것을 할 수는 없다. 제발 도와주세요.

+2

당신은 오류가 어디에서 오는 즉 이후 do_upload/is_uploaded_file 기능을 표시 할 수 있으며 is_uploaded_file에 전달되고 무엇 때문에 분명의 배열이 아닌 오류가 말한대로 문자열입니다. –

+1

오류 메시지가 매우 분명합니다 ... 거기서 시작하십시오. – Sparky

+0

역할을 여러 번 업로드해야만 한 번에 하나의 이미지를 보낼 수 있습니까? –

답변

1

나는 Codeigniter가 실제로 여러 파일 업로드를 처리 할 수 ​​없다는 것을 알고 있습니다. 다중 업로드 클래스는 이전 codeigniter에 대한 것이 었습니다. 그러나 여기에 사용할 수있는 코드가 있습니다. 이것은 기본 PHP 코드를 사용하며, 이것은 codeigniter에서 다중을 업로드하는 유일한 방법입니다.

foreach($_FILES['file']['tmp_name'] as $index => $f) 
    { 
    $target_dir = './assets'; 
    $target_file = $target_dir . basename($_FILES["file"]["name"][$index]); 
    $uploadOk = 1; 
    $files_name = $_FILES["file"]["name"][$index]; 
    // Check if file already exists 
    if (file_exists($target_file)) 
    { 
     //Filexists 
     $uploadOk = 0; 
    } 
    // Check file size 
    if ($_FILES["file"]["size"][$index] > 5000000) 
    { 
     echo "Sorry, your file is too large."; 
     $uploadOk = 0; 
    } 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) 
    { 
     echo "Sorry, your file was not uploaded."; 
     // if everything is ok, try to upload file 
    } 
    else 
    { 
     if (move_uploaded_file($_FILES["file"]["tmp_name"][$index], $target_file)) 
     { 
      //success 
     } 
     else 
     { 
      echo "Sorry, there was an error uploading your file."; 
     } 
    } 
} 
+0

마감 시간에 다른 프로젝트가 있기 때문에 주말에 테스트 할 것입니다. –