Codeigniter和RestServer。如何上传图片? [英] Codeigniter and RestServer. How to upload images?

查看:167
本文介绍了Codeigniter和RestServer。如何上传图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Codeigniter中使用Phils RestServer(请参阅链接)编写API。
我需要一种通过API上传图片的方法。我该怎么办?
它只是简单的做一个POST请求与正确的头(使用的头)?



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



感谢所有人输入!

解决方案

好的,



从休息客户端上传文件到休息服务器 a>



但这两个解决方案都不适合我。



实际工作。



首先,我不知道我的文件是否到达方法,所以我将响应行更改为:

  function enter_post()
{

$ this-> response($ _ FILES);
}

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

您也可以输出:


$ this-> response($ _ SERVER);



$ this-> ($ _POST);


等。



输出:


{file:{name:camel.jpg,type:application / octet-stream ,tmp_name:/ tmp / phpVy8ple,error:0,size: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';
}


I am coding an API using Phils RestServer (see link) in Codeigniter. I need a way to upload images via the API. How can I do that? Is it just as simple as making a POST request with the correct headers (what headers to use)?

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

Thankful for all input!

解决方案

Ok,

There is another solution here: FIle upload from a rest client to a rest server

But neither of these solutions worked for me.

However, this is what worked; actually worked.

First, I wasn't sure if my file was reaching the method, so I changed the response line to:

function enter_post()
    {

        $this->response($_FILES);
    }

Note, this is a great way to test your REST methods.

You can also output:

$this->response($_SERVER);

and

$this->response($_POST);

etc.

I got the following JSON output:

{"file":{"name":"camel.jpg","type":"application/octet-stream","tmp_name":"/tmp/phpVy8ple","error":0,"size":102838}}

So I knew my file was there.

I then changed the method to find and move the file. I used common file access script to get the file from its temp location and move it to a new location:

    $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';        
             }

这篇关于Codeigniter和RestServer。如何上传图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆