小程序如何上传多张图片

wxml 代码


<button bindtap="uploadImg">上传图片</button>


js 代码
uploadImg:function(){
var that = this;
wx.chooseImage({ //选择图片
success: function (res) {
var tempFilePath=res.tempFilePaths;
if (tempFilePath.length>5){ //上传限制
wx.showToast({
title: '上传不超过5张',
icon: 'loading',
duration: 2000
})return;
}
for (var i = 0; i < tempFilePath.length;i++){
var filePath = tempFilePath[i];
wx.uploadFile({
url: ' http://www.***.com/upload.php', //这个是你服务上的php用来处理图片上传的路径(可var_dump($_FILE)出来看看)
filePath: filePath,
name: 'file',
formData: {
'utype': 'upload','i':i
},
method: 'POST', //post必须大写
header: {
'content-type': 'application/x-www-form-urlencoded' //post方法必须写
},
success: function (res) { //成功回调函数
console.log(res);
}
}
}
)}
}
})
}




php 代码 //这边用的是原生php


<?php
$con=mysqli_connect("localhost","root"," ","数据库名"); //连接数据库
if($_POST){
$type=$_POST['utype'];
if($type=='upload'){
$path = 'upload/'. @date ( 'Ymd'); // 接收文件目录
if (! file_exists ( $path )) { //创建文件夹
mkdir ( "$path", 0777, true );
}
$parentDirName = dirname(dirname(__FILE__));
$upload=$parentDirName.$path;
if (file_exists($path . $_FILES["file"]["name"]))
{
echo '找不到图片';
}
else
{
$t=time();
$i=$_POST['i'];
$type=$_FILES["file"]["type"];
$strarr = explode("/",$type);
$type=$strarr[1];
$res= move_uploaded_file($_FILES["file"]["tmp_name"],$path.'/'.$t.$i.'.'.$type );
$photo='/weixin/'.$path.'/'.$t.$i.'.'.$type;
$table=$_POST['tab'];
$sql="insert into $table (imgsrc) values('$photo')";
$res=mysqli_query($con,$sql);
echo $sql;
}
}
}


?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值