【问题标题】:How to upload image via CURL to Codeigniter REST API如何通过 CURL 将图像上传到 Codeigniter REST API
【发布时间】:2012-07-24 08:29:26
【问题描述】:

我在 Codeigniter 中使用 REST API 并尝试设置图像上传方法。我已经成功地使用多部分表单上传,但使用下面的 REST 客户端出现文件类型错误。我认为问题是“file_type”显示为“application/octet-stream”。

(Codeigniter 上传: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] => 
)

【问题讨论】:

  • 你检查过这个吗:stackoverflow.com/questions/7495407/…
  • 我实际上并没有看到它。我没有测试他们讨论的建议,但讨论的问题是一般上传文件类型问题,而不是专门使用 cURL,我已经设法使用 multipart/form 上传。

标签: codeigniter rest curl


【解决方案1】:

看来我需要定义文件类型。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多