首页 技术 正文
技术 2022年11月14日
0 收藏 464 点赞 3,329 浏览 2634 个字

Ajax+PHP实现异步上传多张图片

HTML代码

<!--
date: 2018-04-27 13:46:55
author: 王召波
descride: 多张图片上传
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ajax+PHP实现异步上传多张图片</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>
<style type="text/css">
#feedback{
min-height: 200px;
text-align: center;
border: 1px solid silver;
border-radius: 3px;
}
#feedback img{
margin:3px 10px;
border: 1px solid silver;
border-radius:3px;
padding: 6px;
width: 35%;
height: 85%;
}
#feedback p{
font-family: "微软雅黑";
line-height: 120px;
color: #ccc;
}
.file {
position: relative;
display: inline-block;
border: 1px solid #1ab294;
border-radius: 4px;
padding: 8px 16px;
overflow: hidden;
color: #fff;
text-decoration: none;
text-indent: 0;
line-height: 20px;
color: #1ab294;
} .file input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
}
.box{
margin-top: 10px;
text-align: center;
}
.box a{
margin-left: 10px;
}
</style>
</head>
<body>
<!-- 响应返回数据容器 -->
<div id="feedback">
</div>
<div class="box">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" class="file">选择图片
<input type="file" multiple="multiple" id="inputfile" name="" class="photo">
</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" class="file close">重新选择
<input type="buttom" class="photo">
</a>
</div>
<script type="text/javascript">
$(document).ready(function(){
//响应文件添加成功事件
var feedback = $("#feedback");
$("#inputfile").change(function(){
if (this.files.length>2) {
alert("最多只能选择两张图片");
return false;
}
//创建FormData对象
var data = new FormData();
//为FormData对象添加数据
$.each($('#inputfile')[0].files, function(i, file) {
data.append('ruoshui'+i, file);
});
//追加其他数据
data.append('id',1);
//发送数据
$.ajax({
url:'./upload.php',
type:'post',
data:data,
dataType:'json',
cache: false,
contentType: false,
processData: false,
// contentType: false 和processData: false,这两个参数是为了设置ajax对file文件对象进行序列化
success:function(data){
var str = '';
$.each(data,function(i,n){
str +='<img src="'+n+'">';
});
$("#feedback").append(str);
},
error:function(){
alert('上传出错');
}
});
});
$(".close").on("click",function(){
$("#feedback").empty();
});
});
</script>
</body>
</html>

PHP代码

<?php
/**
* date: 2018-04-27 13:46:55
* author: 王召波
* descride: 多张图片上传
*/
header('content-type:text/html charset:utf-8');
$baseUrl = "./";
$picArr = "";
$index = 0;
foreach($_FILES as $file){
$picName = 'ruoshui' . $index;//对应index.html FomData中的文件命名
$fileName = $_FILES[$picName]['name'];
//文件不存在才上传
if(!file_exists($baseUrl.$fileName)) {
$isMoved = false; //默认上传失败
$MAXIMUM_FILESIZE = 1 * 1024 * 1024;//文件大小限制1M = 1 * 1024 * 1024 B;
$picType = "/^\.(jpg|jpeg|gif|png){1}$/i";
if ($_FILES[$picName]['size'] <= $MAXIMUM_FILESIZE &&
preg_match($picType, strrchr($fileName, '.'))) {
$isMoved = @move_uploaded_file ( $_FILES[$picName]['tmp_name'], $baseUrl.$fileName);//上传文件
}
}
$picArr[] = $baseUrl.$fileName;
$index++;
}
echo json_encode($picArr);

效果图

Ajax+PHP实现异步上传多张图片

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898