微信小程序上传图片到微擎

//图片上传
	public function doPageUpload(){
		global $_GPC, $_W;
		$uptypes = array('image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png');
        $max_file_size = 2000000; //上传文件大小限制, 单位BYTE
        $destination_folder = "../attachment/".$_GPC['m']."/"; //上传文件路径
        if (!is_uploaded_file($_FILES["upfile"]['tmp_name']))
        //是否存在文件
        {
            echo "图片不存在!";
            exit;
        }
        $file = $_FILES["upfile"];
        if ($max_file_size < $file["size"])
        //检查文件大小
        {
            echo "文件太大!";
            exit;
        }
        if (!in_array($file["type"], $uptypes))
        //检查文件类型
        {
            echo "文件类型不符!" . $file["type"];
            exit;
        }

        if (!file_exists($destination_folder)) {
            mkdir($destination_folder);
        }
        $filename = $file["tmp_name"];
        $pinfo = pathinfo($file["name"]);
        $ftype = $pinfo['extension'];
        $destination = $destination_folder . str_shuffle(time() . rand(111111, 999999)) . "." . $ftype;
        if (file_exists($destination) && $overwrite != true) {
            echo "同名文件已经存在了";
            exit;
        }
        if (!move_uploaded_file($filename, $destination)) {
            echo "移动文件出错";
            exit;
        }
        $pinfo = pathinfo($destination);
        $fname = $pinfo['basename'];
        echo $fname;
        @require_once (IA_ROOT . '/framework/function/file.func.php');
        @$filename = $fname;
        @file_remote_upload($filename);
	}

 

selImg(e){
    let that = this;
    let name = e.currentTarget.dataset.name;
    let attachurl = that.data.attachurl;
    let uploadUrl = app.util.url('entry/wxapp/upload',{m:'duson_test'});
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success: function (res) {
            var tempFilePaths = res.tempFilePaths[0]
            wx.uploadFile({
                url: uploadUrl,
                filePath: tempFilePaths,
                name: 'upfile',
                formData: {},
                success: function (res) {
                    console.log(res)
                    if (name == 'head') {
                        let head_img = 'business.head_img';
                        that.setData({ [head_img]: res.data })
                    }
                    if (name == 'door') {
                        let door_photo = 'business.door_photo';
                        that.setData({ [door_photo]:  res.data })
                    }
                },
                fail: function (res) {
                    console.log('失败返回数据',res)
                },
            })
        }
    })
  },

 

你可能感兴趣的:(前端,后端)