restful 上传图片问题 [ 2.0 版本 ]
上传图片
$url = "http://localhost/v1/accident/upload-image";
$file = realpath('_mars.jpg'); //要上传的文件
$data = array(
'uid'=>'123',
'appcode'=>'bhwhc',
'file' => '@'.$file,
'flg' => 2
);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_POST, true );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
var_dump($json_obj);exit;
restful 接口
public function actionUploadImage()
{
if(Yii::$app->request->isGet)
{
$request = Yii::$app->request->get();
}elseif (Yii::$app->request->isPost)
{
$request = Yii::$app->request->post();
}
$file = UploadedFile::getInstance($model,'file');
//return $request;
$uid = $request['uid'];
$appcode = $request['appcode'];
if(empty($uid) || empty($appcode))
{
return ['code'=>'1001','message'=>'参数缺失'];
}
$userinfo = KckpUser::findOne(['app_user_id'=>$uid,'app_code'=>$appcode]);
if(empty($userinfo))
{
$model = new KckpUser();
$model->app_user_id = $uid;
$model->app_code = $appcode;
$model->create_time = date('Y-m-d H:i:s');
$model->save();
}
if($_FILES['file'])
{
//$filename = $_FILES['file']['name'];
return ['code'=>'200','message'=>'有数据上传'];
}else{
return ['code'=>'400','message'=>'无文件上传'];
}
$modelClass = $this->modelClass;
$query = $modelClass::find();
return new ActiveDataProvider([
'query' => $query
]);
}
不知道那里有问题,一直都是没有文件上传
最佳答案
其他 2 个回答
-
zhoupenghui 回答于 2016-08-11 16:58 举报
你先一步步打印,看程序走到哪里了?
qiuch87 济南
注册时间:2013-05-24
最后登录:2024-11-04
在线时长:228小时16分
最后登录:2024-11-04
在线时长:228小时16分
- 粉丝32
- 金钱43435
- 威望0
- 积分45715