PHP--解决微信小程序上传图片(wx.uploadFile)接口代码

下:为小程序.js 的方法:
 chooseImg: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.setData({
          tempFilePath: res.tempFilePaths
        });
        wx.uploadFile({
          url: app.d.ceshiUrl + '/Api/Pintuan/add',//仅为示例,非真实的接口地址//接口连接
          filePath: that.data.tempFilePath['0'],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            console.log(res)
            var data = res.data
            //do something
          }
        })
      }
    })
  },

在对应的接口方法中写:
public function upload($file){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Data/UploadFiles/wechat/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传(子)目录
        // 上传文件
        $info   =   $upload->uploadOne($file);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            return $info['savepath'].$info['savename'];
        }
    }

public function add(){
        $res = $_FILES['file'];
        $res = $this -> upload($res);}
           // var_dump($res);exit;
        // return $this->success($res);
        echo json_encode(array('status'=>0,'err'=>$res));
            exit();

到此,已经完成上传的功能了 。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值