2013-12-12 57 views
2

我想在我的API中使用codeigniter實現一個簡單的圖像上傳功能。到目前爲止,這是我的代碼:使用Codeigniter上傳圖像(API)

function addpicture_post() 
{ 
     $config['upload_path'] = './upload/'; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['max_size'] = '10000'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768'; 
$this->load->library('upload', $config); 

if (! $this->upload->do_upload()) 
    { 
    print_r($this->upload->display_errors('', '')); 
    print_r($this->upload->data()); 
    $info = 'not working'; 
    $this->response($info); 
    } 
} 
} 

當我使用apigee(https://apigee.com/console/others)嘗試一下,如果我不設置任何參數,我有以下消息:

HTTP/1.1 200 OK 狀態:日期: 星期四,2013年12月12日15時三十分十六秒GMT 的Content-Length:保持活動: 超時= 15,最大= 100 內容類型: 應用/ JSON X-技術 - 通過: PHP/5.3.5-1ubuntu7.11 服務器:Apache的 /2.2.17(Ubuntu的)

您沒有選擇要上傳的文件。

到目前爲止這麼好。但是,當我試圖上傳的圖像文件(在遠地點文件attachement參數,我命名爲「userfile的」),我有以下回應:

無效的測試網址

我現在幾個小時相當掙扎,我試圖看看代碼點火器的文檔,但不幸的是,它與API無關,我不知道如何上傳文件而不使用表單。

我不太確定問題來自我用來測試它,apigee的服務,還是問題來自代碼。最後,我的目標是在我正在編寫的iPhone應用程序中實現此功能,但是我希望能夠在嘗試將它包含在我的應用程序中之前對其進行測試。

任何人都可以給我有關文件上傳API中的信息?以及如何正確測試它?謝謝。

+0

提供你的代碼的'視圖' –

+0

除非你在你的視圖做了一些奇怪的事情,我很確定y我們的上傳路徑不需要**。**,所以它應該是_/upload_不是_./upload_ – wiredniko

+0

我已經將./upload/更改爲/ upload /,仍然無法正常工作。我實際上沒有任何看法,因爲我只在使用Apigee在iOS應用中實現它之前嘗試使用它。 – elyanno

回答

1

試試這個,它的工作原理!

*輸入文件名=「圖像」

function upload_image(){ 
$img = array(); // return variable 
$this->load->helper(array('file','directory')); 
if (!empty($collection)) { 
    $path="./uploads/"; 
    if(!is_dir($path)) { 
     mkdir($path); 
    } 
    $config['upload_path'] = $path; /* NB! create this dir! */ 
    $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
    $config['file_name'] = 'image001'; 
    $config['overwrite']=TRUE; 

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


    $configThumb = array(); 
    $configThumb['image_library'] = 'gd2'; 
    $configThumb['source_image'] = ''; 
    $configThumb['create_thumb'] = FALSE; 
    $configThumb['maintain_ratio'] = FALSE; 

     /* Load the image library */ 
    $this->load->library('image_lib'); 

     /* We have 5 files to upload 
     * If you want more - change the 6 below as needed 
     */ 
     /* Handle the file upload */ 
    if (isset($_FILES['image']['tmp_name'])) { 
     $upload = $this->upload->do_upload('image'); 

     /* File failed to upload - continue */ 
     if($upload === FALSE){ 
      $error = array('error' => $this->upload->display_errors()); 
      $data['message']=$error['error']; 
      continue; 
     } 
     /* Get the data about the file */ 
     $data = $this->upload->data(); 
     $img['image']='/'.$data['file_name']; 

    } 

} 
     return $img; 
} 
+0

確認此答案正確! – Benyamin

0

試試這個,它可能會幫助你,我這樣做是用笨框架的工作,這裏是控制器

function do_upload() 
{ 
$config['upload_path']   = './uploads/'; 
$config['allowed_types']  = 'gif|jpg|png'; 
$config['max_size']    = 8000; 
$config['max_width']   = 1024; 
$config['max_height']   = 768; 

$this->load->library('upload', $config); 
if (! $this->upload->do_upload('userfile')) 
{ 
$error = array('error' => $this->upload->display_errors()); 
$this->load->view('upload_form', $error); 
} 
else 
{ 
$data = array('upload_data' => $this->upload->data()); 
$this->load->view('upload_success', $data); 
} 
} 

這裏是一個視圖文件

<?php echo $error;?> 
<?php echo form_open_multipart('upload/do_upload');?> 
<input type="file" name="userfile" size="20" /> 
<input type="submit" value="upload" />