小程序上传图片+php接口

js代码

bindViewTap: function () {
    var that = this;
    console.log(that.data.hits)
    var hit = that.data.hits
    hit = hit+1;
    that.setData({
      hits:hit
    })
    // 多张图片

    if (hit <= 3) {
      wx.chooseImage({
        count: 3,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths;
          that.data.imgsrc.push(tempFilePaths);
          var list = [];
          that.data.imgsrc.forEach(function (item, index) {
            //这里的item就是从数组里拿出来的每一个数组
            var obj = {
              'filesrc': item[0],
            }
            list.push(obj);
            that.setData({
              family: list,
            })
          })

          console.log(that.data.family);
          // 成功之后上传到服务器
          wx.uploadFile({
            url: app.globalData.requesturl,
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'method': 'addImg'
            },
            success: function (res) {
              var data = JSON.parse(res.data);
              console.log(data);
              if(data.status == 3) {
                that.data.iurl.push(data.url);
                var itemp = '';
                that.data.iurl.forEach(function (item2, index2) {
                  //这里的item就是从数组里拿出来的每一个数组
                  // console.log(item2);
                  var list2 = item2 + ',' + itemp;
                  itemp = list2;

                });
                  that.setData({
                    iurlall: itemp,
                  });
                  console.log(that.data.iurlall);
              }else if (data.statu == 2){
                wx.showToast({
                  title: '上传图片不能大于1M!'
                })
              } else if (data.statu == 1) {
                wx.showToast({
                  title: '格式错误',
                })
              } else if (data.statu == 4) {
                wx.showToast({
                  title: '上传失败',
                })
              } else if (data.statu == 6) {
                wx.showToast({
                  title: '上传方式错误',
                })
              }
            } 
            });
        }
      });
    } else {
      wx.showToast({
        title: '最多可上传3张图片',
      })
    }
  },

wxml代码

点我上传

php接口

function addImg() {
        $code = $_FILES['file'];//获取小程序传来的文件
        // return $_FILES['file']['type'];
        // 判断是否是post提交
        if(is_uploaded_file($_FILES['file']['tmp_name'])) {
            //临时文件
            $uploaded_file = $_FILES['file']['tmp_name'];
            // return $uploaded_file;
            // 允许的图片类型
            $judge_img = array("image/gif","image/pjpeg","image/jpeg","image/png","image/jpg");
            // 允许的视频类型
            $judge_vid = array("video/mp4");
            // 判断是图片还是视频
            if(in_array($_FILES['file']['type'],$judge_img)) {
                // 换算字节  1M = 1024KB;1KB = 1000bt;
                if($_FILES["file"]["size"] > 1024000) {
                    // 图片过大
                    $arr = ['status' => 2];
                    return $arr;
                }
                // 图片的路径
                $user_path = DT_ROOT."/include/jjapp/upload/img/";
            }elseif(in_array($_FILES['file']['type'],$judge_vid)) {
                if($_FILES["file"]["size"] > 2048*1000) {
                    // 视频过大
                    $arr = ['status' => 2];
                    return $arr;
                }
                // 视频的路径
                $user_path = DT_ROOT."/include/jjapp/upload/video/";
            }else {
                // 格式错误
                $arr = ['status' => 1];
                return $arr;
            }
            $file_true_name = $_FILES['file']['name']; 
            $move_to_file = $user_path.time()."-".date("Y-m-d").substr($file_true_name,strrpos($file_true_name,"."));  
            if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) {  
                // 上传成功
                $arr = ['status' => 3, 'url' => $move_to_file];
                return $arr;
            } else {  
                // 上传失败网络错误,请重新上传
                $arr = ['status' => 4];
                return $arr;
            }  
        } else { 
            // 上传方式错误
            $arr = ['status' => 6];
            return $arr;
        }
    }

你可能感兴趣的:(php,小程序,前端,js)