## 文件结构
```
|- index.html
|- index.php
```
## 具体代码
```
|- index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script>
function setfile(){
var fd = new FormData;
var pic = document.getElementsByTagName('input')[0].files[0];
fd.append('pic',pic);
var xhr = new XMLHttpRequest();
xhr.open('POST','index.php',true);
xhr.send(fd);
}
</script>
<body>
<input type="file" name="pic" onchange="setfile()">
</body>
</html>
```
```
|- index.php
<?php
/**
* User: 三千
* Date: 16/2/25
* Time: 上午12:02
*/
move_uploaded_file($_FILES['pic']['tmp_name'],$_FILES['pic']['name']);
```
##可能遇到的问题
在unix平台下,可能会上传失败,错误提示如下:
```ppp
Warning: move_uploaded_file(demo.jpg): failed to open stream: Permission denied in
```
原因是权限不够
解决方法命令行输入:``` chmod -R 777 dir ```
dir是指你的程序所在的目录