2016-08-11 11:47:03 3224次浏览 3条回答 0 悬赏 20 金钱

上传图片

$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
         ]);
     }

不知道那里有问题,一直都是没有文件上传

最佳答案

  • dingjj2010 发布于 2016-08-11 17:17 举报

    查看php版本,5.5前是用'@'+文件全路径,5.5后用CURLFile类。

    1 条回复
    回复于 2016-08-11 18:01 回复

    谢谢,真的解决了

您需要登录后才可以回答。登录 | 立即注册
qiuch87
副董事长

qiuch87 济南

注册时间:2013-05-24
最后登录:2024-11-04
在线时长:228小时16分
  • 粉丝32
  • 金钱43435
  • 威望0
  • 积分45715

热门问题