Codeigniter和RestServer.如何上传图片?

Jon*_*ark 8 rest upload codeigniter

我正在使用Codeigniter中的Phils RestServer(请参阅链接)编写API.我需要一种通过API上传图像的方法.我怎样才能做到这一点?它是否就像使用正确的标题(要使用哪些标题)发出POST请求一样简单?

https://github.com/philsturgeon/codeigniter-restserver

感谢所有输入!

ssa*_*man 12

好,

这里有另一种解决方案:从休息客户端上传到休息服务器

但这些解决方案都不适合我.

但是,这是有效的; 实际上工作.

首先,我不确定我的文件是否到达方法,因此我将响应行更改为:

function enter_post()
    {

        $this->response($_FILES);
    }
Run Code Online (Sandbox Code Playgroud)

注意,这是测试REST方法的好方法.

你还可以输出:

$这 - >响应($ _ SERVER);

$这 - >响应($ _ POST);

等等

我得到了以下JSON输出:

{ "文件":{ "名称": "camel.jpg", "类型": "应用/八位字节流", "tmp_name的值": "/ TMP/phpVy8ple", "错误":0, "大小":102838 }}

所以我知道我的档案在那里.

然后我更改了方法以查找并移动文件.我使用通用文件访问脚本从其临时位置获取文件并将其移动到新位置:

    $uploaddir = '/home/me/public_html/uploads/';

    $uploadfile = $uploaddir . basename($_FILES['file']['name']);

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        $data['status'] = 'uploaded';       
            } else {
        $data['status'] =  'failed';        
             }
Run Code Online (Sandbox Code Playgroud)

  • 伟大的答案先生,萨尔曼,真的很健康 (2认同)

sek*_*ati 5

您实际上可以使用CodeIgniter的本机文件上传类来方便上传Phil's Rest-Server,如下所示:

/**
 * REST API Upload using native CI Upload class.
 * @param userfile - multipart/form-data file
 * @param submit - must be non-null value.
 * @return upload data array || error string
 */
function upload_post(){
    if( ! $this->post('submit')) {
        $this->response(NULL, 400);
    }
    $this->load->library('upload');

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


jon*_*ohn 0

在这里查看 amazon s3 是如何做到的http://www.anyexample.com/programming/php/uploading_files_to_amazon_s3_with_rest_api.xml

看起来像在客户端定义文件类型,然后将其加载到变量中file_get_contents($file_path),然后使用示例中定义的适当标头发送它。

然后在服务器上,我假设它将file_put_contents()与请求正文一起使用来保存文件。