PHP如何接收多组图片文件,小程序表单 和多张图片上传 ,php接收表单信息并且把图片存入服务器文件夹,图片路径存入数据库...

var adds = {};

Page({

data: {

img_arr: [],

formdata: '',

},

formSubmit: function (e) {

var id = e.target.id

adds = e.detail.value;

adds.nowTime =new Date().getTime();

this.upload()

},

upload: function () {

var that = this

for (var i = 0; i < this.data.img_arr.length; i++) {

adds.index = i;

console.log(adds)

wx.uploadFile({

url: 'http://localhost/upImg/up_img.php',

filePath: that.data.img_arr[i],

name: 'file',

formData: adds,

success: function (res) {

console.log('1111111111',res)

if (res) {

wx.showToast({

title: '已提交发布!',

duration: 3000

});

}

}

})

}

this.setData({

formdata: ''

})

},

upimg: function () {

var that = this;

if (this.data.img_arr.length < 3) {

wx.chooseImage({

sizeType: ['original', 'compressed'],

success: function (res) {

that.setData({

img_arr: that.data.img_arr.concat(res.tempFilePaths)

})

}

})

} else {

wx.showToast({

title: '最多上传三张图片',

icon: 'loading',

duration: 3000

});

}

},

})

2

姓名:

电话:

发布

3

0){

echo "错误: " . $_FILES["file"]["error"] . "

";

}

$name = $_POST['name'];

$phone = $_POST['phone'];

$index = $_POST['index'];

$nowTime = $_POST['nowTime'];

$img = $_FILES['file']['name'];

if($index==0){

//插入数据到数据库

$sql = "insert into test (name,phone,img,nowTime) values('$name','$phone','$img','$nowTime')";

$retval = mysqli_query( $conn, $sql );

if(! $retval )

{

die('无法插入数据: ' . mysqli_error($conn));

}

echo "数据插入成功\n";

mysqli_close($conn);

move_uploaded_file($_FILES["file"]["tmp_name"],

"upload/" . $_FILES["file"]["name"]);

}else{

//提交图片

$img = $_FILES['file']['name'];

$result = mysqli_query($conn,"SELECT * FROM test

WHERE nowTime='$nowTime'");

$ab;

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

{

if($row["img"]!=""){

$ab =$row["img"].','.$img;

}else{

$ab =$img;

}

//修改数据库字段

mysqli_query($conn,"UPDATE test SET img = '$ab'

WHERE nowTime = '$nowTime'");

echo $ab;

}

move_uploaded_file($_FILES["file"]["tmp_name"],

"upload/" . $_FILES["file"]["name"]);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用HTML5的FormData对象来实现多文件,然后使用Ajax将数据发送到服务器。在服务器端,您可以使用Java的Servlet来处理上文件,并将文件路径保存到数据库中。以下是一个简的示例代码: HTML代码: ```html <form id="myForm" enctype="multipart/form-data"> <input type="file" name="file1"> <input type="file" name="file2"> <input type="button" value="Upload" onclick="uploadFiles()"> </form> ``` JavaScript代码: ```javascript function uploadFiles() { var form = document.getElementById("myForm"); var formData = new FormData(form); var xhr = new XMLHttpRequest(); xhr.open("POST", "uploadServlet", true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { alert(xhr.responseText); } }; xhr.send(formData); } ``` Java代码: ```java @WebServlet("/uploadServlet") @MultipartConfig public class UploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String file1Path = saveFile(request.getPart("file1")); String file2Path = saveFile(request.getPart("file2")); // 将文件路径保存到数据库中 // ... response.getWriter().write("Files uploaded successfully!"); } private String saveFile(Part part) throws IOException { String fileName = getFileName(part); String filePath = "C:/uploads/" + fileName; part.write(filePath); return filePath; } private String getFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); String[] tokens = contentDisposition.split(";"); for (String token : tokens) { if (token.trim().startsWith("filename")) { return token.substring(token.indexOf("=") + 2, token.length() - 1); } } return ""; } } ``` 请注意,此示例代码仅供参考,您需要根据自己的需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值