文件上传与Restler V3 [英] File Upload with Restler v3
问题描述
就在最近,对于的multipart / form-data的支持上传加入Restler V3(的来源),但我不能让它工作。在我的index.php文件,我说:
$ R 34 GT; setSupportedFormats('JsonFormat','UploadFormat');
当我发布一个txt文件,我得到以下错误(这是意料之中的,因为允许的格式图像/ JPEG,图像/ PNG'默认的:
错误:{
code:403,
:不支持的文件类型(文本/纯)禁止:消息
}
但是,当我发布一个.jpg文件,我得到以下错误,而不是:
错误:{
code:404,
消息:未找到
}
我在想什么?这里是我的功能:
函数上传(){
如果(空($ request_data)){
抛出新RestException(412,是的RequestData空);
}
返回数组('upload_status'=>'图像上传成功');
}
我想它了!所有我需要一个后()
的功能!对于谁跑入我遇到了同样的问题,任何人,这是我上传一个文件Restler 3解决方法:
的index.php
< PHP
require_once'../vendor/restler.php';
使用Luracast \\ Restler \\ Restler; $ R =新Restler();
$ R-> addAPIClass('上传');
$ R-> setSupportedFormats('JsonFormat','UploadFormat'); $ R->手柄();
upload.php的
< PHP
类上传{
函数的get(){
如果(空($ request_data)){
抛出新RestException(412,是的RequestData空);
}
} 功能后($ request_data = NULL){
返回数组('upload_status'=>'图像上传成功!');
}
}
Just recently, support for multipart/form-data upload was added to Restler v3 (source) but I can't get it to work. In my index.php file, I've added:
$r->setSupportedFormats('JsonFormat', 'UploadFormat');
When I post a .txt file, I get the following error (which is expected, since the default 'allowed' format is 'image/jpeg', 'image/png':
"error": {
"code": 403,
"message": "Forbidden: File type (text/plain) is not supported."
}
But when I post a .jpg file, I get the following error instead:
"error": {
"code": 404,
"message": "Not Found"
}
What am I missing? Here is my function:
function upload() {
if (empty($request_data)) {
throw new RestException(412, "requestData is null");
}
return array('upload_status'=>'image uploaded successfully');
}
I figured it out! All I needed a post()
function! For anyone who runs into the same issue I ran into, here is my solution for uploading a file with Restler 3:
index.php
<?php
require_once '../vendor/restler.php';
use Luracast\Restler\Restler;
$r = new Restler();
$r->addAPIClass('Upload');
$r->setSupportedFormats('JsonFormat', 'UploadFormat');
$r->handle();
Upload.php
<?php
class Upload {
function get(){
if (empty($request_data)) {
throw new RestException(412, "requestData is null");
}
}
function post($request_data=NULL) {
return array('upload_status'=>'image uploaded successfully!');
}
}
这篇关于文件上传与Restler V3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!