方法一
對(duì)php.ini進(jìn)行修改
file_uploads = on; //是否允許通過(guò)http上傳文件的開(kāi)關(guān)(默認(rèn)為開(kāi))
upload_max_filesize = 8m // PHP允許最大上傳文件大小
post_max_size = 8m; //表單post提交允許最大上傳文件大小
max_execution_time = 30; // php頁(yè)面運(yùn)行最大時(shí)間(默認(rèn)30s)
max_input_time = 60; // php頁(yè)面接收數(shù)據(jù)允許最大時(shí)間(默認(rèn)60s)
內(nèi)存限制= 8m; // php占用最大內(nèi)存(默認(rèn)8m)
方法二
實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳
思路:
將大文件看成是由多個(gè)小文件組成的(從1kb到500kb為1文件阅茶,從501kb到1000kb為2文件,以此類推;利用javascript函數(shù)slice()完成)
將這些小文件在后臺(tái)組合成一個(gè)大文件(利用php函數(shù))
引入代碼
html+javascript:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>斷點(diǎn)續(xù)傳</title>
</head>
<body>
<div id="drop_area" style="border:3px dashed silver;width:200px; height:200px">
將圖片拖拽到此
</div>
<progress value="0" max="10" id="prouploadfile"></progress>
<span id="persent">0%</span>
<br />
<!--<button onclick="xhr2()">ajax上傳</button>-->
<button onclick="stopup()" id="stop">上傳</button>
<script>
//拖拽上傳開(kāi)始
//-1.禁止瀏覽器打開(kāi)文件行為
document.addEventListener("drop",function(e){ //拖離
e.preventDefault();
})
document.addEventListener("dragleave",function(e){ //拖后放
e.preventDefault();
})
document.addEventListener("dragenter",function(e){ //拖進(jìn)
e.preventDefault();
})
document.addEventListener("dragover",function(e){ //拖來(lái)拖去
e.preventDefault();
})
//上傳進(jìn)度
var pro = document.getElementById('prouploadfile');
var persent = document.getElementById('persent');
function clearpro(){
pro.value=0;
persent.innerHTML="0%";
}
//2.拖拽
var stopbutton = document.getElementById('stop');
var resultfile=""
var box = document.getElementById('drop_area'); //拖拽區(qū)域
box.addEventListener("drop",function(e){
var fileList = e.dataTransfer.files; //獲取文件對(duì)象
console.log(fileList)
//檢測(cè)是否是拖拽文件到頁(yè)面的操作
if(fileList.length == 0){
return false;
}
//拖拉圖片到瀏覽器,可以實(shí)現(xiàn)預(yù)覽功能
//規(guī)定視頻格式
//in_array
Array.prototype.S=String.fromCharCode(2);
Array.prototype.in_array=function(e){
var r=new RegExp(this.S+e+this.S);
return (r.test(this.S+this.join(this.S)+this.S));
};
var video_type=["video/mp4","video/ogg"];
//創(chuàng)建一個(gè)url連接,供src屬性引用
var fileurl = window.URL.createObjectURL(fileList[0]);
if(fileList[0].type.indexOf('image') === 0){ //如果是圖片
var str="";
document.getElementById('drop_area').innerHTML=str;
}else if(video_type.in_array(fileList[0].type)){ //如果是規(guī)定格式內(nèi)的視頻
var str="<video width='200px' height='200px' controls='controls' src='"+fileurl+"'></video>";
document.getElementById('drop_area').innerHTML=str;
}else{ //其他格式桌吃,輸出文件名
//alert("不預(yù)覽");
var str="文件名字:"+fileList[0].name;
document.getElementById('drop_area').innerHTML=str;
}
resultfile = fileList[0];
console.log(resultfile);
//切片計(jì)算
filesize= resultfile.size;
setsize=500*1024;
filecount = filesize/setsize;
//console.log(filecount)
//定義進(jìn)度條
pro.max=parseInt(Math.ceil(filecount));
i =getCookie(resultfile.name);
i = (i!=null && i!="")?parseInt(i):0
if(Math.floor(filecount)<i){
alert("已經(jīng)完成");
pro.value=i+1;
persent.innerHTML="100%";
}else{
alert(i);
pro.value=i;
p=parseInt(i)*100/Math.ceil(filecount)
persent.innerHTML=parseInt(p)+"%";
}
},false);
//3.ajax上傳
var stop=1;
function xhr2(){
if(stop==1){
return false;
}
if(resultfile==""){
alert("請(qǐng)選擇文件")
return false;
}
i=getCookie(resultfile.name);
console.log(i)
i = (i!=null && i!="")?parseInt(i):0
if(Math.floor(filecount)<parseInt(i)){
alert("已經(jīng)完成");
return false;
}else{
//alert(i)
}
var xhr = new XMLHttpRequest();//第一步
//新建一個(gè)FormData對(duì)象
var formData = new FormData(); //++++++++++
//追加文件數(shù)據(jù)
//改變進(jìn)度條
pro.value=i+1;
p=parseInt(i+1)*100/Math.ceil(filecount)
persent.innerHTML=parseInt(p)+"%";
//進(jìn)度條
if((filesize-i*setsize)>setsize){
blobfile= resultfile.slice(i*setsize,(i+1)*setsize);
}else{
blobfile= resultfile.slice(i*setsize,filesize);
formData.append('lastone', Math.floor(filecount));
}
formData.append('file', blobfile);
//return false;
formData.append('blobname', i); //++++++++++
formData.append('filename', resultfile.name); //++++++++++
//post方式
xhr.open('POST', 'duandian.php'); //第二步驟
//發(fā)送請(qǐng)求
xhr.send(formData); //第三步驟
stopbutton.innerHTML = "暫停"
//ajax返回
xhr.onreadystatechange = function(){ //第四步
if ( xhr.readyState == 4 && xhr.status == 200 ) {
console.log( xhr.responseText );
if(i<filecount){
xhr2();
}else{
i=0;
}
}
};
//設(shè)置超時(shí)時(shí)間
xhr.timeout = 20000;
xhr.ontimeout = function(event){
alert('請(qǐng)求超時(shí),網(wǎng)絡(luò)擁堵培廓!低于25K/s');
}
i=i+1;
setCookie(resultfile.name,i,365)
}
//設(shè)置cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/")
}
//獲取cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
function stopup(){
if(stop==1){
stop = 0
xhr2();
}else{
stop = 1
stopbutton.innerHTML = "繼續(xù)"
}
}
</script>
</body>
</html>
php:
<?php
//$name=$_POST['username'];
$dir=$_POST['filename'];
$dir="uploads/".md5($dir);
file_exists($dir) or mkdir($dir,0777,true);
$path=$dir."/".$_POST['blobname'];
//print_r($_FILES["file"]);
move_uploaded_file($_FILES["file"]["tmp_name"],$path);
if(isset($_POST['lastone'])){
echo $_POST['lastone'];
$count=$_POST['lastone'];
$fp = fopen($_POST['filename'],"abw");
for($i=0;$i<=$count;$i++){
$handle = fopen($dir."/".$i,"rb");
fwrite($fp,fread($handle,filesize($dir."/".$i)));
fclose($handle);
}
fclose($fp);
}
?>