修改php配置文件(php.ini)蔼两,改變文件上傳大小
? ? ? ? ?配置項(xiàng) post_max_size =1024M
? ? ? ? ? ? ? ? ? ? ?upload_max_filesize =1024M
多文件上傳
form表單中添加多個(gè)input[type='file']標(biāo)簽
或
加上multiple = 'multiple'屬性
多文件無刷新上傳(ajax)
<input type='file' id='file' name='file[]' multiple = 'multiple'>
//上傳按鈕改為button疾捍,給點(diǎn)擊事件
<button onclick='upload()'>多文件上傳</button>
//js
<script>
function upload() {
var xhr=new XMLHttpRequest();//第一步
//新建一個(gè)FormData對(duì)象
var formData=new FormData();//++++++++++
//獲取id=file文件
var newfile=document.getElementById('file').files;
//? ? ? ? console.log(newfile);
//? ? ? ? return;
//追加文件數(shù)據(jù)
//? ? ? ? formData.append('file', newfile);單個(gè)文件
for(i=0;i<newfile.length;i++){
? ? ? formData.append("file["+i+"]",newfile[i]);//++++++++++
}
//post方式
xhr.open('POST','upload.php');//第二步驟
//發(fā)送請(qǐng)求
xhr.send(formData);//第三步驟
//ajax返回
xhr.onreadystatechange=function(){//第四步
? ? ? ? ? if(xhr.readyState==4&&xhr.status==200) {
? ? ? ? ? ? ? ? console.log(xhr.responseText);
? ? ? ? ? }
?};
//設(shè)置超時(shí)時(shí)間
xhr.timeout=10000;
xhr.ontimeout=function(event){
? ? ? ? ? ? alert('請(qǐng)求超時(shí)镐依!');
? ? ? ?}
}
//php文件
/**
* 文件上傳保存
*/
print_r($_FILES);
for($i=0;$i<count($_FILES["file"]['name']);$i++){
$name=$_FILES["file"]["name"][$i];
move_uploaded_file($_FILES["file"]["tmp_name"][$i],iconv("UTF-8","gb2312",$name));
}