2012-07-25 60 views
1

我在Codeigniter中使用REST API並嘗試設置圖像上傳方法。我設法使用多部分表單很好地上傳,但使用下面的REST客戶端獲取文件類型錯誤。我認爲問題是「file_type」顯示爲「application/octet-stream」。如何通過CURL將圖像上載到Codeigniter REST API

(笨上傳:http://codeigniter.com/user_guide/libraries/file_uploading.html

REST方法

function do_upload_post() 
{ 
    $config['upload_path'] = './savefiles/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '100'; 
    $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()); 
    } 
} 

REST客戶

function curl_upload(){ 
    $url = 'http://localhost/api/file/do_upload'; 
    $file = 'C:\inetpub\wwwroot\ds_site\designs\pics\file.jpg'; 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POST, true); 
    $post = array(
     "userfile" => "@$file" 
    ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch); 
    echo $response; 

    curl_close($ch); 
} 

輸出

The filetype you are attempting to upload is not allowed. 
Array 
(
    [file_name] => aliens.jpg 
    [file_type] => application/octet-stream 
    [file_path] => C:/inetpub/wwwroot/ds_site/savefiles/ 
    [full_path] => C:/inetpub/wwwroot/ds_site/savefiles/aliens.jpg 
    [raw_name] => aliens 
    [orig_name] => 
    [client_name] => aliens.jpg 
    [file_ext] => .jpg 
    [file_size] => 103745 
    [is_image] => 
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
) 
+0

你檢查這一個:http://stackoverflow.com/questions/7495407/uploading-in-codeigniter-the-filetype-you-are-attempting-to-upload-is-not-allo? – SCO 2012-07-25 20:04:40

+0

我沒有看到它。我沒有測試過他們所討論的建議,但討論的問題是一般的上傳文件類型問題,而不是專門使用cURL,並且我設法使用multipart/form很好地上傳。 – 2012-07-25 20:43:16

回答

1

看來我需要定義文件類型。

"userfile" => "@$file;type=image/jpeg"