php文件上传图片更改头像(包括ajax异步上传)
第一种:非ajax:
示例图:
上传表单:MAX_FILE_SIZE:表单上限制文件上传大小,这个值小于upload_max_filesize。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>上传</title>
<style type="text/css">
img{
border-radius: 50%;
width: 100px;
height: 100px;
cursor: pointer;
}
input.submit{
cursor: pointer;
margin: 20px;
}
#up{
border: 1px solid #ccc;
border-radius: 5px;
height: 30px;
line-height: 30px;
background: #f60;
width: 150px;
cursor: pointer;
text-align: center;
}
img{
border-radius: 50%;
width: 120px;
height: 120px;
}
</style>
</head>
<body>
<div id="up">选择图片</div>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="userfile" style="display: none;" id="file">
<input type="submit" value="确定上传" name="send" class="submit">
</form>
<!-- 这里的头像图片地址是从数据库里取的默认头像地址上传成功后要更改临时数据库的值和临时显示的头像
-->
<img src="uploads/index.jpg" id="face">
<script type="text/javascript">
var up = document.getElementById('up');
var file = document.getElementById('file');
up.onclick = function(){
// 事件委托,意味着点击up这个元素就是点击file这个元素
file.click();
}
</script>
</body>
</html>
php:要放在html下面因为上传成功后要更改某些dom元素的值
<?php
if(isset($_POST['send'])){
upload();
}
// 封装成函数
function upload($_path='uploads'){
// 判断文件类型
//设置上传图片的类型
$_files = array('im