2014-01-25 88 views
2

在我的CodeIgniter項目中我在創建項目期間上傳文件。這裏的上傳功能:在CodeIgniter中上傳多個文件不起作用

public function addProcess() 
{ 
echo 'test'; 
    if($this->input->server('REQUEST_METHOD') === 'POST') 
    { 
     $this->form_validation->set_rules('album', 'Album', 'trim|required|callback_check_select', 'xss_clean'); 

     if ($this->form_validation->run() === FALSE) 
     { 
      $data['albums'] = $this->album_model->albumList(); 
      $this->adminTheme('admin/addSong',$data); 
     } 
     else 
     { 
       $error = 0; 
       $this->load->library('upload'); 
       $total_count_of_files = count($_FILES['userfile']['name']); 

       $album = $this->input->post('album'); 
       $songs = $this->input->post('song'); 
       $artist = $this->input->post('artist'); 


       for($i=0; $i< $total_count_of_files; $i++) 
       { 

        if(!$songs[$i]) 
         $songs[$i] = "Track-".($i+1); 
        if(!$artist[$i]) 
         $artist[$i] = " "; 

       $_FILES['file']['name'] = $_FILES['userfile']['name'][$i]; 
       $_FILES['file']['type'] = $_FILES['userfile']['type'][$i]; 
       $_FILES['file']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i]; 
       $_FILES['file']['error']  = $_FILES['userfile']['error'][$i]; 
       $_FILES['file']['size'] = $_FILES['userfile']['size'][$i]; 

       $config['upload_path'] = './upload/'; 
       $config['allowed_types'] = 'jpg|jpeg|gif|png|mp3'; 
       $config['max_size']  = '0'; 
       $config['overwrite']  = FALSE; 
       $config['encrypt_name'] = TRUE; 

       $this->upload->initialize($config); 

         if($this->upload->do_upload('file')) 
         { 
          $msg = $this->upload->data(); 
          $name = $msg['file_name']; 

          $error += 0; 
          $this->song_model->add($songs[$i], $artist[$i], $name, $album); 

         }else{ 
          $errorStr[] = "$songs[$i] Upload Faild"; 
          $error += 1; 
         } 
       } 


      //$this->album_model->add(); 
      if($error == 0) 
      { 
       //$this->msg('admin/song','Successfully added..Redirecting now...'); 
      } 
      else 
      { 
       $this->msg('admin/song','Successfully added..Redirecting now...'); 
      } 
     } 
    } 
    else 
    { 
     redirect('admin/album'); 
    } 
} 

爲doupload

public function do_upload() 
    { 
     $upload_path_url = base_url().'uploads/'; 

     $config['upload_path'] = FCPATH.'uploads/'; 
     $config['allowed_types'] = 'jpg|jpeg|gif|png|mp3'; 
     $config['max_size'] = '30000'; 

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

     if (! $this->upload->do_upload()) { 
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('upload', $error); 

     } else { 
      $data = $this->upload->data(); 
     $info->name = $data['file_name']; 
      $info->size = $data['file_size']; 
     $info->type = $data['file_type']; 
      $info->url = $upload_path_url .$data['file_name']; 

     $info->thumbnail_url = $upload_path_url .$data['file_name']; 
      $info->delete_url = base_url().'upload/deleteImage/'.$data['file_name']; 
      $info->delete_type = 'DELETE'; 


     if (IS_AJAX) { 
      echo json_encode(array($info)); 

     } else { 
      $file_data['upload_data'] = $this->upload->data(); 
      $this->load->view('admin/upload_success', $file_data); 
     } 
    } 
} 

我的表單代碼

<div id="container"> 
     <h2>Song Name - Add New</h2> 
     <div class="error"> 
     <?php echo validation_errors(); ?> 
     </div> 
     <?php $attributes = array('id' => 'myform'); ?> 
     <?php echo form_open_multipart('admin/song/addProcess',$attributes); ?> 
     <div class="line"> 
      <label style="width: 160px;">Album:</label> 
      <select class="category" name="album" style="width: 280px;"> 
      <option value="select">Select Album</option> 
       <?php foreach($albums as $album): ?> 
       <option value="<?php echo $album['id']; ?>"> 

        <?php echo $album['root_name']." --> ".$album['category_name']." --> ".$album['album_name']; ?></option> 
       <?php endforeach ?> 
      </select> 

        <input class="form-submit" style="margin-top: 6px;" type="button" id="more_fields" onclick="add_fields();" value="Add More" /> 
     </div> 
     <!--end line--> 





       <div id="nameFields"> 
        <div class="line"> 
         <input type="text" name="song[]" class="input1" style="width: 180px;" placeholder="Song Name" /> 
         <input type="text" name="artist[]" class="input1" style="width: 180px;" placeholder="Artist Name" /> 
         <input type="file" name="userfile[]" class="category" style="width:80px;" /> 
        </div> 
      <!--end line--> 
       </div> 


     <input type="submit" value="Save" class="form-submit" style="margin-right: 180px;" /> 

     </form> 
    </div> 
    <!--end container--> 



<script type="text/javascript"> 
        function add_fields() { 
           document.getElementById('nameFields').innerHTML += "<div class='line'><input type='text' name='song[]' class='input1' style='width: 180px;' placeholder='Song Name' /><input type='text' name='artist[]' class='input1' style='width: 180px;' placeholder='Artist Name' /><input type='file' name='userfile[]' class='category' style='width:80px;' /></div>"; 
         } 


</script> 

然而,這並不做任何其他功能。這些文件沒有被上傳。即使是空目錄也沒有被創建。有人能幫我找到錯誤嗎?

謝謝。

+0

可以添加你的表單代碼?添加了@kumar_v的 –

+0

表單代碼 – shanto

回答

0

在你addProcess代碼,刪除下面的代碼

$_FILES['file']['name'] = $_FILES['userfile']['name'][$i]; 
$_FILES['file']['type'] = $_FILES['userfile']['type'][$i]; 
$_FILES['file']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i]; 
$_FILES['file']['error']  = $_FILES['userfile']['error'][$i]; 
$_FILES['file']['size'] = $_FILES['userfile']['size'][$i]; 

,並嘗試

foreach ($_FILES as $fieldname => $fileObject) 
{ 
    if (!empty($fileObject['name'])) 
    { 
     $this->upload->initialize($config); 
     if (!$this->upload->do_upload($fieldname)) 
     { 
      $errors[] = $this->upload->display_errors(); 

     } 
     else 
     { 
      //success code 
     } 
    } 
}