2012-12-19 212 views
4

我絕對堅持這一點...檢查所有相關的stackoverflow帖子,沒有任何幫助。在Codeigniter中使用Phil的REST服務器/客戶端設置,可以插入普通條目,但無法上傳多個圖像,甚至無法上傳一張圖像。Codeigniter REST上傳多張圖片

我真的不能確定如何調試REST部分,它只是不返回任何內容。

我有這樣的陣列從視圖進來:

Array 
(
    [1] => Array 
     (
      [name] => sideshows.jpg 
      [type] => image/jpeg 
      [tmp_name] => /Applications/MAMP/tmp/php/phppYycdA 
      [error] => 0 
      [size] => 967656 
     ) 

    [2] => Array 
     (
      [name] => the-beer-scale.jpg 
      [type] => image/jpeg 
      [tmp_name] => /Applications/MAMP/tmp/php/phpCsiunQ 
      [error] => 0 
      [size] => 742219 
     ) 

    [3] => Array 
     (
      [name] => the-little-lace.jpg 
      [type] => image/jpeg 
      [tmp_name] => /Applications/MAMP/tmp/php/phpXjT7WL 
      [error] => 0 
      [size] => 939963 
     ) 

    [4] => Array 
     (
      [name] => varrstoen-australia.jpg 
      [type] => image/jpeg 
      [tmp_name] => /Applications/MAMP/tmp/php/phpcHrJXe 
      [error] => 0 
      [size] => 2204400 
     ) 

) 

我使用這個輔助方法,我發現理清多文件上傳:

function multifile_array() 
{ 
    if(count($_FILES) == 0) 
     return; 

    $files = array(); 
    $all_files = $_FILES['files']['name']; 
    $i = 0; 

    foreach ($all_files as $filename) { 
     $files[++$i]['name'] = $filename; 
     $files[$i]['type'] = current($_FILES['files']['type']); 
     next($_FILES['files']['type']); 
     $files[$i]['tmp_name'] = current($_FILES['files']['tmp_name']); 
     next($_FILES['files']['tmp_name']); 
     $files[$i]['error'] = current($_FILES['files']['error']); 
     next($_FILES['files']['error']); 
     $files[$i]['size'] = current($_FILES['files']['size']); 
     next($_FILES['files']['size']); 
    } 

    $_FILES = $files; 

} 

此功能正在API控制器中調用:

public function my_file_upload_post() { 

     if(! $this->post('submit')) { 
      $this->response(NULL, 400); 
     } 

     $data = $this->post('data'); 
     multifile_array(); 

     $foldername = './uploads/' . $this->post('property_id'); 

     if(!file_exists($foldername) && !is_dir($foldername)) { 
      mkdir($foldername, 0750, true); 
     } 

     $config['upload_path'] = $foldername; 
     $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt'; 
     $config['max_size'] = '10240'; 

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

     foreach ($data as $file => $file_data) { 
      $this->upload->do_upload($file); 

      //echo '<pre>'; 
      //print_r($this->upload->data()); 
      //echo '</pre>'; 

     } 


     if (! $this->upload->do_upload()) { 
      return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404); 
     } else { 
      $upload = $this->upload->data(); 
      return $this->response($upload, 200); 

     } 



    } 

我很高興與大家分享我的代碼,我確實設法讓多個文件上傳不用擔心,但只是嘗試使用API​​進行設置,所以我可以從外部網站,內部或iPhone調用它。幫助將不勝感激。

乾杯

更新:

下面是從API控制器代碼:

function upload_post() { 

     $foldername = './uploads/' . $this->post('property_id'); 

     if(!file_exists($foldername) && !is_dir($foldername)) { 
      mkdir($foldername, 0750, true); 
     } 

     $config['upload_path'] = $foldername; 
     $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt'; 
     $config['max_size'] = '10240'; 

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

     if (! $this->upload->do_upload()) 
     { 
      //return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404); 
      return $this->response(array('error' => var_export($this->post('file'), true)), 404); 

     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 
      return $this->response(array('error' => $data['upload_data']), 404); 
     } 
return $this->response(array('success' => 'successfully uploaded'), 200);  
} 

這個工作,如果你去直接從形式API,但你需要在瀏覽器中輸入用戶名和密碼。如果我通過控制器運行它,那麼它無法找到圖像。

回答