小程序Laravel图片上传

 //前端
add_prove: function () {
var _this = this;
var provePaths = this.data.provePaths;
wx.chooseImage({
count: 1, // 选择照片的数量(默认9张)
sizeType: [ 'original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: [ 'album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片

var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: '',
filePath: tempFilePaths[ 0],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log(res);
var data = res.data
_this.setData({
img: data,
})
}
})
}
})
},
//后端
public function uniqid_img()
{
    $Path = "/public/upload/ActivityImg/";
    if (!empty($_FILES['file'])) {
        //获取扩展名
        $exename = $this->getExeName($_FILES['file']['name']);
        if ($exename != 'png' && $exename != 'jpg' && $exename != 'gif') {
            exit('不允许的扩展名');
        }
        $fileName = $_SERVER['DOCUMENT_ROOT'] . $Path . date('Ym');//文件路径
        $upload_name = '/img_' . date("YmdHis") . rand(0, 100) . '.' . $exename;//文件名加后缀
        if (!file_exists($fileName)) {
            //进行文件创建
            mkdir($fileName, 0777, true);
        }
        $imageSavePath = $fileName . $upload_name;
        if (move_uploaded_file($_FILES['file']['tmp_name'], $imageSavePath)) {
            echo $Path . date('Ym') . $upload_name;
        }
    }
}

public function getExeName($fileName)
{
    $pathinfo = pathinfo($fileName);
    return strtolower($pathinfo['extension']);
}


你可能感兴趣的:(小程序Laravel图片上传)