问题描述
我在Codeigniter中使用Phils RestServer(请参阅链接)编写API。
我需要一种通过API上传图片的方法。我该怎么办?
它只是简单的做一个POST请求与正确的头(使用的头)?
感谢所有人输入!
好的,
但这两个解决方案都不适合我。
实际工作。
首先,我不知道我的文件是否到达方法,所以我将响应行更改为:
function enter_post()
{
$ this-> response($ _ FILES);
}
注意,这是测试REST方法的好方法。 >
您也可以输出:
和
等。
输出:
我知道我的文件在那里。
然后我改变了方法来找到并移动文件。我使用通用文件访问脚本从其临时位置获取文件并将其移动到新位置:
$ 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:
and
etc.
I got the following JSON output:
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。如何上传图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!